21 #include "SphereModel.h"
23 #include "Mathematics.h"
44 LOGE(
"radius value has to be greater than zero.");
49 if (numberOfSamples <= 0)
51 LOGE(
"numberOfSamples value has to be greater than zero.");
55 if (coordinates == NULL)
57 LOGE(
"Cannot use null pointer while calculating coordinates.");
63 const float epsilon = 0.001f;
65 int indexOfSphereArray = 0;
67 float maxTheta = (2.0f *
M_PI);
69 float thetaStep = maxTheta /
float(numberOfSamples);
71 float radiusStep = (2 * radius) /
float(numberOfSamples-1);
77 const int numberOfSphereCoordinates = numberOfSamples * numberOfSamples * 3;
80 *coordinates = (
float*) malloc (numberOfSphereCoordinates *
sizeof(
float));
82 if (*coordinates == NULL)
84 LOGE(
"Could not allocate memory for result array.");
90 for (
float r = -radius;
92 r = -radius + radiusIndex * radiusStep)
103 for (
float theta = 0.0
f;
105 theta = thetaIndex * thetaStep)
108 float x = sqrt((radius * radius) - (
r *
r)) * cosf(theta);
109 float y = sqrt((radius * radius) - (r * r)) * sinf(theta);
112 (*coordinates)[indexOfSphereArray] =
x;
113 indexOfSphereArray++;
115 (*coordinates)[indexOfSphereArray] =
y;
116 indexOfSphereArray++;
118 (*coordinates)[indexOfSphereArray] = z;
119 indexOfSphereArray++;
127 if (numberOfCoordinates != NULL)
129 *numberOfCoordinates = numberOfSphereCoordinates;
138 LOGE(
"radius value has to be greater than zero.");
143 if (numberOfSamples <= 0)
145 LOGE(
"numberOfSamples value has to be greater than zero.");
149 if (coordinates == NULL)
151 LOGE(
"Cannot use null pointer while calculating coordinates.");
156 float* pointCoordinates = NULL;
158 int sphereTriangleCoordinateIndex = 0;
160 int iterationIndex = 1;
162 int numberOfCoordinatesCreatedInOneLoop = 18;
175 if (pointCoordinates == NULL)
177 LOGE(
"Could not get coordinates of points which make up a sphere.");
181 *coordinates = (
float*) malloc (numberOfSphereTriangleCoordinates *
sizeof(
float));
183 if (*coordinates == NULL)
185 LOGE(
"Could not allocate memory for result array.");
190 for (
int pointIndex = 0;
191 pointIndex < (numberOfSphereTriangleCoordinates / numberOfCoordinatesCreatedInOneLoop) * 3;
200 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex];
201 sphereTriangleCoordinateIndex++;
203 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 1];
204 sphereTriangleCoordinateIndex++;
206 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 2];
207 sphereTriangleCoordinateIndex++;
210 if ((iterationIndex % numberOfSamples) == 0 )
220 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex - 3 * (
numberOfSamples) + 3];
221 sphereTriangleCoordinateIndex++;
223 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex - 3 * (
numberOfSamples) + 3 + 1];
224 sphereTriangleCoordinateIndex++;
226 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex - 3 * (
numberOfSamples) + 3 + 2];
227 sphereTriangleCoordinateIndex++;
236 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3];
237 sphereTriangleCoordinateIndex++;
239 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 + 1];
240 sphereTriangleCoordinateIndex++;
242 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 + 2];
243 sphereTriangleCoordinateIndex++;
254 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3];
255 sphereTriangleCoordinateIndex++;
257 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 + 1];
258 sphereTriangleCoordinateIndex++;
260 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 + 2];
261 sphereTriangleCoordinateIndex++;
271 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + numberOfSamples * 3 + 3];
272 sphereTriangleCoordinateIndex++;
274 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + numberOfSamples * 3 + 3 + 1];
275 sphereTriangleCoordinateIndex++;
277 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + numberOfSamples * 3 + 3 + 2];
278 sphereTriangleCoordinateIndex++;
288 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex];
289 sphereTriangleCoordinateIndex++;
291 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 1];
292 sphereTriangleCoordinateIndex++;
294 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 2];
295 sphereTriangleCoordinateIndex++;
298 if ((iterationIndex % numberOfSamples) == 0 )
306 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3];
307 sphereTriangleCoordinateIndex++;
309 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 + 1];
310 sphereTriangleCoordinateIndex++;
312 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 + 2];
313 sphereTriangleCoordinateIndex++;
325 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 * numberOfSamples + 3];
326 sphereTriangleCoordinateIndex++;
328 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 * numberOfSamples + 3 + 1];
329 sphereTriangleCoordinateIndex++;
331 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 * numberOfSamples + 3 + 2];
332 sphereTriangleCoordinateIndex++;
340 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 *
numberOfSamples];
341 sphereTriangleCoordinateIndex++;
343 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 *numberOfSamples + 1];
344 sphereTriangleCoordinateIndex++;
346 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 * numberOfSamples + 2];
347 sphereTriangleCoordinateIndex++;
354 if (numberOfCoordinates != NULL)
360 free(pointCoordinates);
361 pointCoordinates = NULL;
static void getTriangleRepresentation(const float radius, const int numberOfSamples, int *numberOfCoordinates, float **coordinates)
Create triangular representation of a sphere.
static void getPointRepresentation(const float radius, const int numberOfSamples, int *numberOfCoordinates, float **coordinates)
Compute coordinates of points which make up a sphere.
int numberOfSphereTriangleCoordinates
#define M_PI
The value of pi.
GLint GLint GLint GLint GLint x