summaryrefslogtreecommitdiff
path: root/thirdparty/assimp/code/StandardShapes.cpp
blob: 2e5100130f0f4bfddccf369f371a91b6a12cfeb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------

Copyright (c) 2006-2019, assimp team


All rights reserved.

Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other
  materials provided with the distribution.

* Neither the name of the assimp team, nor the names of its
  contributors may be used to endorse or promote products
  derived from this software without specific prior
  written permission of the assimp team.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------
*/

/** @file   StandardShapes.cpp
 *  @brief  Implementation of the StandardShapes class
 *
 *  The primitive geometry data comes from
 *  http://geometrictools.com/Documentation/PlatonicSolids.pdf.
 */

#include <assimp/StandardShapes.h>
#include <assimp/StringComparison.h>
#include <stddef.h>
#include <assimp/Defines.h>
#include <assimp/mesh.h>

namespace Assimp    {


# define ADD_TRIANGLE(n0,n1,n2) \
    positions.push_back(n0); \
    positions.push_back(n1); \
    positions.push_back(n2);

#   define ADD_PENTAGON(n0,n1,n2,n3,n4) \
    if (polygons) \
    { \
        positions.push_back(n0); \
        positions.push_back(n1); \
        positions.push_back(n2); \
        positions.push_back(n3); \
        positions.push_back(n4); \
    } \
    else \
    { \
        ADD_TRIANGLE(n0, n1, n2) \
        ADD_TRIANGLE(n0, n2, n3) \
        ADD_TRIANGLE(n0, n3, n4) \
    }

#   define ADD_QUAD(n0,n1,n2,n3) \
    if (polygons) \
    { \
        positions.push_back(n0); \
        positions.push_back(n1); \
        positions.push_back(n2); \
        positions.push_back(n3); \
    } \
    else \
    { \
        ADD_TRIANGLE(n0, n1, n2) \
        ADD_TRIANGLE(n0, n2, n3) \
    }


// ------------------------------------------------------------------------------------------------
// Fast subdivision for a mesh whose verts have a magnitude of 1
void Subdivide(std::vector<aiVector3D>& positions)
{
    // assume this to be constant - (fixme: must be 1.0? I think so)
    const ai_real fl1 = positions[0].Length();

    unsigned int origSize = (unsigned int)positions.size();
    for (unsigned int i = 0 ; i < origSize ; i+=3)
    {
        aiVector3D& tv0 = positions[i];
        aiVector3D& tv1 = positions[i+1];
        aiVector3D& tv2 = positions[i+2];

        aiVector3D a = tv0, b = tv1, c = tv2;
        aiVector3D v1 = aiVector3D(a.x+b.x, a.y+b.y, a.z+b.z).Normalize()*fl1;
        aiVector3D v2 = aiVector3D(a.x+c.x, a.y+c.y, a.z+c.z).Normalize()*fl1;
        aiVector3D v3 = aiVector3D(b.x+c.x, b.y+c.y, b.z+c.z).Normalize()*fl1;

        tv0 = v1; tv1 = v3; tv2 = v2; // overwrite the original
        ADD_TRIANGLE(v1, v2, a);
        ADD_TRIANGLE(v2, v3, c);
        ADD_TRIANGLE(v3, v1, b);
    }
}

// ------------------------------------------------------------------------------------------------
// Construct a mesh from given vertex positions
aiMesh* StandardShapes::MakeMesh(const std::vector<aiVector3D>& positions,
    unsigned int numIndices)
{
    if (positions.empty() || !numIndices) return NULL;

    // Determine which kinds of primitives the mesh consists of
    aiMesh* out = new aiMesh();
    switch (numIndices) {
        case 1:
            out->mPrimitiveTypes = aiPrimitiveType_POINT;
            break;
        case 2:
            out->mPrimitiveTypes = aiPrimitiveType_LINE;
            break;
        case 3:
            out->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
            break;
        default:
            out->mPrimitiveTypes = aiPrimitiveType_POLYGON;
            break;
    };

    out->mNumFaces = (unsigned int)positions.size() / numIndices;
    out->mFaces = new aiFace[out->mNumFaces];
    for (unsigned int i = 0, a = 0; i < out->mNumFaces;++i) {
        aiFace& f = out->mFaces[i];
        f.mNumIndices = numIndices;
        f.mIndices = new unsigned int[numIndices];
        for (unsigned int j = 0; i < numIndices; ++i, ++a) {
            f.mIndices[j] = a;
        }
    }
    out->mNumVertices = (unsigned int)positions.size();
    out->mVertices = new aiVector3D[out->mNumVertices];
    ::memcpy(out->mVertices,&positions[0],out->mNumVertices*sizeof(aiVector3D));

    return out;
}

// ------------------------------------------------------------------------------------------------
// Construct a mesh with a specific shape (callback)
aiMesh* StandardShapes::MakeMesh ( unsigned int (*GenerateFunc)(
    std::vector<aiVector3D>&))
{
    std::vector<aiVector3D> temp;
    unsigned num = (*GenerateFunc)(temp);
    return MakeMesh(temp,num);
}

// ------------------------------------------------------------------------------------------------
// Construct a mesh with a specific shape (callback)
aiMesh* StandardShapes::MakeMesh ( unsigned int (*GenerateFunc)(
    std::vector<aiVector3D>&, bool))
{
    std::vector<aiVector3D> temp;
    unsigned num = (*GenerateFunc)(temp,true);
    return MakeMesh(temp,num);
}

// ------------------------------------------------------------------------------------------------
// Construct a mesh with a specific shape (callback)
aiMesh* StandardShapes::MakeMesh (unsigned int num,  void (*GenerateFunc)(
    unsigned int,std::vector<aiVector3D>&))
{
    std::vector<aiVector3D> temp;
    (*GenerateFunc)(num,temp);
    return MakeMesh(temp,3);
}

// ------------------------------------------------------------------------------------------------
// Build an incosahedron with points.magnitude == 1
unsigned int StandardShapes::MakeIcosahedron(std::vector<aiVector3D>& positions)
{
    positions.reserve(positions.size()+60);

    const ai_real t = ( ai_real( 1.0 )+ ai_real( 2.236067977 ) ) / ai_real( 2.0 );
    const ai_real s = std::sqrt(ai_real(1.0) + t*t);

    const aiVector3D v0  = aiVector3D(t,1.0, 0.0)/s;
    const aiVector3D v1  = aiVector3D(-t,1.0, 0.0)/s;
    const aiVector3D v2  = aiVector3D(t,-1.0, 0.0)/s;
    const aiVector3D v3  = aiVector3D(-t,-1.0, 0.0)/s;
    const aiVector3D v4  = aiVector3D(1.0, 0.0, t)/s;
    const aiVector3D v5  = aiVector3D(1.0, 0.0,-t)/s;
    const aiVector3D v6  = aiVector3D(-1.0, 0.0,t)/s;
    const aiVector3D v7  = aiVector3D(-1.0, 0.0,-t)/s;
    const aiVector3D v8  = aiVector3D(0.0, t, 1.0)/s;
    const aiVector3D v9  = aiVector3D(0.0,-t, 1.0)/s;
    const aiVector3D v10 = aiVector3D(0.0, t,-1.0)/s;
    const aiVector3D v11 = aiVector3D(0.0,-t,-1.0)/s;

    ADD_TRIANGLE(v0,v8,v4);
    ADD_TRIANGLE(v0,v5,v10);
    ADD_TRIANGLE(v2,v4,v9);
    ADD_TRIANGLE(v2,v11,v5);

    ADD_TRIANGLE(v1,v6,v8);
    ADD_TRIANGLE(v1,v10,v7);
    ADD_TRIANGLE(v3,v9,v6);
    ADD_TRIANGLE(v3,v7,v11);

    ADD_TRIANGLE(v0,v10,v8);
    ADD_TRIANGLE(v1,v8,v10);
    ADD_TRIANGLE(v2,v9,v11);
    ADD_TRIANGLE(v3,v11,v9);

    ADD_TRIANGLE(v4,v2,v0);
    ADD_TRIANGLE(v5,v0,v2);
    ADD_TRIANGLE(v6,v1,v3);
    ADD_TRIANGLE(v7,v3,v1);

    ADD_TRIANGLE(v8,v6,v4);
    ADD_TRIANGLE(v9,v4,v6);
    ADD_TRIANGLE(v10,v5,v7);
    ADD_TRIANGLE(v11,v7,v5);
    return 3;
}

// ------------------------------------------------------------------------------------------------
// Build a dodecahedron with points.magnitude == 1
unsigned int StandardShapes::MakeDodecahedron(std::vector<aiVector3D>& positions,
    bool polygons /*= false*/)
{
    positions.reserve(positions.size()+108);

    const ai_real a = ai_real( 1.0 ) / ai_real(1.7320508);
    const ai_real b = std::sqrt(( ai_real( 3.0 )- ai_real( 2.23606797))/ ai_real( 6.0) );
    const ai_real c = std::sqrt(( ai_real( 3.0 )+ ai_real( 2.23606797f))/ ai_real( 6.0) );

    const aiVector3D v0  = aiVector3D(a,a,a);
    const aiVector3D v1  = aiVector3D(a,a,-a);
    const aiVector3D v2  = aiVector3D(a,-a,a);
    const aiVector3D v3  = aiVector3D(a,-a,-a);
    const aiVector3D v4  = aiVector3D(-a,a,a);
    const aiVector3D v5  = aiVector3D(-a,a,-a);
    const aiVector3D v6  = aiVector3D(-a,-a,a);
    const aiVector3D v7  = aiVector3D(-a,-a,-a);
    const aiVector3D v8  = aiVector3D(b,c,0.0);
    const aiVector3D v9  = aiVector3D(-b,c,0.0);
    const aiVector3D v10 = aiVector3D(b,-c,0.0);
    const aiVector3D v11 = aiVector3D(-b,-c,0.0);
    const aiVector3D v12 = aiVector3D(c, 0.0, b);
    const aiVector3D v13 = aiVector3D(c, 0.0, -b);
    const aiVector3D v14 = aiVector3D(-c, 0.0, b);
    const aiVector3D v15 = aiVector3D(-c, 0.0, -b);
    const aiVector3D v16 = aiVector3D(0.0, b, c);
    const aiVector3D v17 = aiVector3D(0.0, -b, c);
    const aiVector3D v18 = aiVector3D(0.0, b, -c);
    const aiVector3D v19 = aiVector3D(0.0, -b, -c);

    ADD_PENTAGON(v0, v8, v9, v4, v16);
    ADD_PENTAGON(v0, v12, v13, v1, v8);
    ADD_PENTAGON(v0, v16, v17, v2, v12);
    ADD_PENTAGON(v8, v1, v18, v5, v9);
    ADD_PENTAGON(v12, v2, v10, v3, v13);
    ADD_PENTAGON(v16, v4, v14, v6, v17);
    ADD_PENTAGON(v9, v5, v15, v14, v4);

    ADD_PENTAGON(v6, v11, v10, v2, v17);
    ADD_PENTAGON(v3, v19, v18, v1, v13);
    ADD_PENTAGON(v7, v15, v5, v18, v19);
    ADD_PENTAGON(v7, v11, v6, v14, v15);
    ADD_PENTAGON(v7, v19, v3, v10, v11);
    return (polygons ? 5 : 3);
}

// ------------------------------------------------------------------------------------------------
// Build an octahedron with points.magnitude == 1
unsigned int StandardShapes::MakeOctahedron(std::vector<aiVector3D>& positions)
{
    positions.reserve(positions.size()+24);

    const aiVector3D v0  = aiVector3D(1.0, 0.0, 0.0) ;
    const aiVector3D v1  = aiVector3D(-1.0, 0.0, 0.0);
    const aiVector3D v2  = aiVector3D(0.0, 1.0, 0.0);
    const aiVector3D v3  = aiVector3D(0.0, -1.0, 0.0);
    const aiVector3D v4  = aiVector3D(0.0, 0.0, 1.0);
    const aiVector3D v5  = aiVector3D(0.0, 0.0, -1.0);

    ADD_TRIANGLE(v4,v0,v2);
    ADD_TRIANGLE(v4,v2,v1);
    ADD_TRIANGLE(v4,v1,v3);
    ADD_TRIANGLE(v4,v3,v0);

    ADD_TRIANGLE(v5,v2,v0);
    ADD_TRIANGLE(v5,v1,v2);
    ADD_TRIANGLE(v5,v3,v1);
    ADD_TRIANGLE(v5,v0,v3);
    return 3;
}

// ------------------------------------------------------------------------------------------------
// Build a tetrahedron with points.magnitude == 1
unsigned int StandardShapes::MakeTetrahedron(std::vector<aiVector3D>& positions)
{
    positions.reserve(positions.size()+9);

    const ai_real invThree = ai_real( 1.0 ) / ai_real( 3.0 );
    const ai_real a = ai_real( 1.41421 ) * invThree;
    const ai_real b = ai_real( 2.4494 ) * invThree;

    const aiVector3D v0  = aiVector3D(0.0,0.0,1.0);
    const aiVector3D v1  = aiVector3D(2*a,0,-invThree );
    const aiVector3D v2  = aiVector3D(-a,b,-invThree );
    const aiVector3D v3  = aiVector3D(-a,-b,-invThree );

    ADD_TRIANGLE(v0,v1,v2);
    ADD_TRIANGLE(v0,v2,v3);
    ADD_TRIANGLE(v0,v3,v1);
    ADD_TRIANGLE(v1,v3,v2);
    return 3;
}

// ------------------------------------------------------------------------------------------------
// Build a hexahedron with points.magnitude == 1
unsigned int StandardShapes::MakeHexahedron(std::vector<aiVector3D>& positions,
    bool polygons /*= false*/)
{
    positions.reserve(positions.size()+36);
    const ai_real length = ai_real(1.0)/ai_real(1.73205080);

    const aiVector3D v0  = aiVector3D(-1.0,-1.0,-1.0)*length;
    const aiVector3D v1  = aiVector3D(1.0,-1.0,-1.0)*length;
    const aiVector3D v2  = aiVector3D(1.0,1.0,-1.0)*length;
    const aiVector3D v3  = aiVector3D(-1.0,1.0,-1.0)*length;
    const aiVector3D v4  = aiVector3D(-1.0,-1.0,1.0)*length;
    const aiVector3D v5  = aiVector3D(1.0,-1.0,1.0)*length;
    const aiVector3D v6  = aiVector3D(1.0,1.0,1.0)*length;
    const aiVector3D v7  = aiVector3D(-1.0,1.0,1.0)*length;

    ADD_QUAD(v0,v3,v2,v1);
    ADD_QUAD(v0,v1,v5,v4);
    ADD_QUAD(v0,v4,v7,v3);
    ADD_QUAD(v6,v5,v1,v2);
    ADD_QUAD(v6,v2,v3,v7);
    ADD_QUAD(v6,v7,v4,v5);
    return (polygons ? 4 : 3);
}

// Cleanup ...
#undef ADD_TRIANGLE
#undef ADD_QUAD
#undef ADD_PENTAGON

// ------------------------------------------------------------------------------------------------
// Create a subdivision sphere
void StandardShapes::MakeSphere(unsigned int    tess,
    std::vector<aiVector3D>& positions)
{
    // Reserve enough storage. Every subdivision
    // splits each triangle in 4, the icosahedron consists of 60 verts
    positions.reserve(positions.size()+60 * integer_pow(4, tess));

    // Construct an icosahedron to start with
    MakeIcosahedron(positions);

    // ... and subdivide it until the requested output
    // tessellation is reached
    for (unsigned int i = 0; i<tess;++i)
        Subdivide(positions);
}

// ------------------------------------------------------------------------------------------------
// Build a cone
void StandardShapes::MakeCone(ai_real height,ai_real radius1,
    ai_real radius2,unsigned int tess,
    std::vector<aiVector3D>& positions,bool bOpen /*= false */)
{
    // Sorry, a cone with less than 3 segments makes ABSOLUTELY NO SENSE
    if (tess < 3 || !height)
        return;

    size_t old = positions.size();

    // No negative radii
    radius1 = std::fabs(radius1);
    radius2 = std::fabs(radius2);

    ai_real halfHeight = height / ai_real(2.0);

    // radius1 is always the smaller one
    if (radius2 > radius1)
    {
        std::swap(radius2,radius1);
        halfHeight = -halfHeight;
    }
    else old = SIZE_MAX;

    // Use a large epsilon to check whether the cone is pointy
    if (radius1 < (radius2-radius1)*10e-3)radius1 = 0.0;

    // We will need 3*2 verts per segment + 3*2 verts per segment
    // if the cone is closed
    const unsigned int mem = tess*6 + (!bOpen ? tess*3 * (radius1 ? 2 : 1) : 0);
    positions.reserve(positions.size () + mem);

    // Now construct all segments
    const ai_real angle_delta = (ai_real)AI_MATH_TWO_PI / tess;
    const ai_real angle_max   = (ai_real)AI_MATH_TWO_PI;

    ai_real s = 1.0; // std::cos(angle == 0);
    ai_real t = 0.0; // std::sin(angle == 0);

    for (ai_real angle = 0.0; angle < angle_max; )
    {
        const aiVector3D v1 = aiVector3D (s * radius1, -halfHeight, t * radius1 );
        const aiVector3D v2 = aiVector3D (s * radius2,  halfHeight, t * radius2 );

        const ai_real next = angle + angle_delta;
        ai_real s2 = std::cos(next);
        ai_real t2 = std::sin(next);

        const aiVector3D v3 = aiVector3D (s2 * radius2,  halfHeight, t2 * radius2 );
        const aiVector3D v4 = aiVector3D (s2 * radius1, -halfHeight, t2 * radius1 );

        positions.push_back(v1);
        positions.push_back(v2);
        positions.push_back(v3);
        positions.push_back(v4);
        positions.push_back(v1);
        positions.push_back(v3);

        if (!bOpen)
        {
            // generate the end 'cap'
            positions.push_back(aiVector3D(s * radius2,  halfHeight, t * radius2 ));
            positions.push_back(aiVector3D(s2 * radius2,  halfHeight, t2 * radius2 ));
            positions.push_back(aiVector3D(0.0, halfHeight, 0.0));


            if (radius1)
            {
                // generate the other end 'cap'
                positions.push_back(aiVector3D(s * radius1,  -halfHeight, t * radius1 ));
                positions.push_back(aiVector3D(s2 * radius1,  -halfHeight, t2 * radius1 ));
                positions.push_back(aiVector3D(0.0, -halfHeight, 0.0));

            }
        }
        s = s2;
        t = t2;
        angle = next;
    }

    // Need to flip face order?
    if ( SIZE_MAX != old )  {
        for (size_t p = old; p < positions.size();p += 3) {
            std::swap(positions[p],positions[p+1]);
        }
    }
}

// ------------------------------------------------------------------------------------------------
// Build a circle
void StandardShapes::MakeCircle(ai_real radius, unsigned int tess,
    std::vector<aiVector3D>& positions)
{
    // Sorry, a circle with less than 3 segments makes ABSOLUTELY NO SENSE
    if (tess < 3 || !radius)
        return;

    radius = std::fabs(radius);

    // We will need 3 vertices per segment
    positions.reserve(positions.size()+tess*3);

    const ai_real angle_delta = (ai_real)AI_MATH_TWO_PI / tess;
    const ai_real angle_max   = (ai_real)AI_MATH_TWO_PI;

    ai_real s = 1.0; // std::cos(angle == 0);
    ai_real t = 0.0; // std::sin(angle == 0);

    for (ai_real angle = 0.0; angle < angle_max;  )
    {
        positions.push_back(aiVector3D(s * radius,0.0,t * radius));
        angle += angle_delta;
        s = std::cos(angle);
        t = std::sin(angle);
        positions.push_back(aiVector3D(s * radius,0.0,t * radius));

        positions.push_back(aiVector3D(0.0,0.0,0.0));
    }
}

} // ! Assimp