@@ -141,22 +141,24 @@ static void buildTriangleAdjacencySparse(TriangleAdjacency2& adjacency, const un
141
141
}
142
142
}
143
143
144
- static void computeBoundingSphere (float result[4 ], const float points[][ 3 ] , size_t count)
144
+ static void computeBoundingSphere (float result[4 ], const float * points, size_t count, size_t points_stride )
145
145
{
146
146
assert (count > 0 );
147
147
148
+ size_t points_stride_float = points_stride / sizeof (float );
149
+
148
150
// find extremum points along all 3 axes; for each axis we get a pair of points with min/max coordinates
149
151
size_t pmin[3 ] = {0 , 0 , 0 };
150
152
size_t pmax[3 ] = {0 , 0 , 0 };
151
153
152
154
for (size_t i = 0 ; i < count; ++i)
153
155
{
154
- const float * p = points[i] ;
156
+ const float * p = points + i * points_stride_float ;
155
157
156
158
for (int axis = 0 ; axis < 3 ; ++axis)
157
159
{
158
- pmin[axis] = (p[axis] < points[pmin[axis]][ axis]) ? i : pmin[axis];
159
- pmax[axis] = (p[axis] > points[pmax[axis]][ axis]) ? i : pmax[axis];
160
+ pmin[axis] = (p[axis] < points[pmin[axis] * points_stride_float + axis]) ? i : pmin[axis];
161
+ pmax[axis] = (p[axis] > points[pmax[axis] * points_stride_float + axis]) ? i : pmax[axis];
160
162
}
161
163
}
162
164
@@ -166,8 +168,8 @@ static void computeBoundingSphere(float result[4], const float points[][3], size
166
168
167
169
for (int axis = 0 ; axis < 3 ; ++axis)
168
170
{
169
- const float * p1 = points[ pmin[axis]] ;
170
- const float * p2 = points[ pmax[axis]] ;
171
+ const float * p1 = points + pmin[axis] * points_stride_float ;
172
+ const float * p2 = points + pmax[axis] * points_stride_float ;
171
173
172
174
float d2 = (p2[0 ] - p1[0 ]) * (p2[0 ] - p1[0 ]) + (p2[1 ] - p1[1 ]) * (p2[1 ] - p1[1 ]) + (p2[2 ] - p1[2 ]) * (p2[2 ] - p1[2 ]);
173
175
@@ -179,16 +181,16 @@ static void computeBoundingSphere(float result[4], const float points[][3], size
179
181
}
180
182
181
183
// use the longest segment as the initial sphere diameter
182
- const float * p1 = points[ pmin[paxis]] ;
183
- const float * p2 = points[ pmax[paxis]] ;
184
+ const float * p1 = points + pmin[paxis] * points_stride_float ;
185
+ const float * p2 = points + pmax[paxis] * points_stride_float ;
184
186
185
187
float center[3 ] = {(p1[0 ] + p2[0 ]) / 2 , (p1[1 ] + p2[1 ]) / 2 , (p1[2 ] + p2[2 ]) / 2 };
186
188
float radius = sqrtf (paxisd2) / 2 ;
187
189
188
190
// iteratively adjust the sphere up until all points fit
189
191
for (size_t i = 0 ; i < count; ++i)
190
192
{
191
- const float * p = points[i] ;
193
+ const float * p = points + i * points_stride_float ;
192
194
float d2 = (p[0 ] - center[0 ]) * (p[0 ] - center[0 ]) + (p[1 ] - center[1 ]) * (p[1 ] - center[1 ]) + (p[2 ] - center[2 ]) * (p[2 ] - center[2 ]);
193
195
194
196
if (d2 > radius * radius)
@@ -857,13 +859,13 @@ meshopt_Bounds meshopt_computeClusterBounds(const unsigned int* indices, size_t
857
859
858
860
// compute cluster bounding sphere; we'll use the center to determine normal cone apex as well
859
861
float psphere[4 ] = {};
860
- computeBoundingSphere (psphere, corners[0 ], triangles * 3 );
862
+ computeBoundingSphere (psphere, corners[0 ][ 0 ] , triangles * 3 , sizeof ( float ) * 3 );
861
863
862
864
float center[3 ] = {psphere[0 ], psphere[1 ], psphere[2 ]};
863
865
864
866
// treating triangle normals as points, find the bounding sphere - the sphere center determines the optimal cone axis
865
867
float nsphere[4 ] = {};
866
- computeBoundingSphere (nsphere, normals, triangles);
868
+ computeBoundingSphere (nsphere, normals[ 0 ] , triangles, sizeof ( float ) * 3 );
867
869
868
870
float axis[3 ] = {nsphere[0 ], nsphere[1 ], nsphere[2 ]};
869
871
float axislength = sqrtf (axis[0 ] * axis[0 ] + axis[1 ] * axis[1 ] + axis[2 ] * axis[2 ]);
@@ -987,20 +989,8 @@ meshopt_Bounds meshopt_computeSphereBounds(const float* positions, size_t count,
987
989
if (count == 0 )
988
990
return bounds;
989
991
990
- meshopt_Allocator allocator;
991
- float * centers = allocator.allocate <float >(count * 3 ); // TBD
992
-
993
- for (size_t i = 0 ; i < count; ++i)
994
- {
995
- const float * position = positions + i * (positions_stride / sizeof (float ));
996
-
997
- centers[i * 3 + 0 ] = position[0 ];
998
- centers[i * 3 + 1 ] = position[1 ];
999
- centers[i * 3 + 2 ] = position[2 ];
1000
- }
1001
-
1002
992
float psphere[4 ] = {};
1003
- computeBoundingSphere (psphere, ( float (*)[ 3 ])centers , count);
993
+ computeBoundingSphere (psphere, positions , count, positions_stride );
1004
994
1005
995
float pradius = 0 ;
1006
996
0 commit comments