2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

geometrydatafactory.cpp 15.5 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
11
12
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22
23
24
25
26
// 
// ================================================================================================

#include "geometrydatafactory.h"

27
#include "cgt/assert.h"
28

29
30
#include "core/tools/teapot.h"

31
32
namespace campvis {

33
34
    FaceGeometry* GeometryDataFactory::createQuad(const cgt::vec3& llf, const cgt::vec3& urb, const cgt::vec3& texLlf, const cgt::vec3& texUrb) {
        std::vector<cgt::vec3> vertices, texCorods;
35

36
37
38
39
        vertices.push_back(cgt::vec3(llf.x, llf.y, llf.z));
        vertices.push_back(cgt::vec3(urb.x, llf.y, llf.z));
        vertices.push_back(cgt::vec3(urb.x, urb.y, llf.z));
        vertices.push_back(cgt::vec3(llf.x, urb.y, llf.z));
40

41
42
43
44
        texCorods.push_back(cgt::vec3(texLlf.x, texLlf.y, texLlf.z));
        texCorods.push_back(cgt::vec3(texUrb.x, texLlf.y, texLlf.z));
        texCorods.push_back(cgt::vec3(texUrb.x, texUrb.y, texLlf.z));
        texCorods.push_back(cgt::vec3(texLlf.x, texUrb.y, texLlf.z));
45
46
47
48

        return new FaceGeometry(vertices, texCorods);
    }

49
50
51
52
53
    MeshGeometry* GeometryDataFactory::createCube(const cgt::Bounds& bounds, const cgt::Bounds& texBounds) {
        const cgt::vec3& llf = bounds.getLLF();
        const cgt::vec3& urb = bounds.getURB();
        const cgt::vec3& tLlf = texBounds.getLLF();
        const cgt::vec3& tUrb = texBounds.getURB();
54
55

        // not the most elegant method, but it works...
56
        std::vector<cgt::vec3> vertices, texCoords;
57
58
59
        std::vector<FaceGeometry> faces;

        // front
60
61
62
63
64
65
66
67
68
        texCoords.push_back(cgt::vec3(tLlf.x, tUrb.y, tLlf.z));
        vertices.push_back(cgt::vec3(llf.x, urb.y, llf.z));
        texCoords.push_back(cgt::vec3(tUrb.x, tUrb.y, tLlf.z));
        vertices.push_back(cgt::vec3(urb.x, urb.y, llf.z));
        texCoords.push_back(cgt::vec3(tUrb.x, tLlf.y, tLlf.z));
        vertices.push_back(cgt::vec3(urb.x, llf.y, llf.z));
        texCoords.push_back(cgt::vec3(tLlf.x, tLlf.y, tLlf.z));
        vertices.push_back(cgt::vec3(llf.x, llf.y, llf.z));
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<cgt::vec4>(), std::vector<cgt::vec3>(4, cgt::vec3(0.f, 0.f, -1.f))));
69
70
71
72
        vertices.clear();
        texCoords.clear();

        // right
73
74
75
76
77
78
79
80
81
        texCoords.push_back(cgt::vec3(tUrb.x, tUrb.y, tLlf.z));
        vertices.push_back(cgt::vec3(urb.x, urb.y, llf.z));
        texCoords.push_back(cgt::vec3(tUrb.x, tUrb.y, tUrb.z));
        vertices.push_back(cgt::vec3(urb.x, urb.y, urb.z));
        texCoords.push_back(cgt::vec3(tUrb.x, tLlf.y, tUrb.z));
        vertices.push_back(cgt::vec3(urb.x, llf.y, urb.z));
        texCoords.push_back(cgt::vec3(tUrb.x, tLlf.y, tLlf.z));
        vertices.push_back(cgt::vec3(urb.x, llf.y, llf.z));
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<cgt::vec4>(), std::vector<cgt::vec3>(4, cgt::vec3(1.f, 0.f, 0.f))));
82
83
84
85
        vertices.clear();
        texCoords.clear();

        // top
86
87
88
89
90
91
92
93
94
        texCoords.push_back(cgt::vec3(tLlf.x, tUrb.y, tUrb.z));
        vertices.push_back(cgt::vec3(llf.x, urb.y, urb.z));
        texCoords.push_back(cgt::vec3(tUrb.x, tUrb.y, tUrb.z));
        vertices.push_back(cgt::vec3(urb.x, urb.y, urb.z));
        texCoords.push_back(cgt::vec3(tUrb.x, tUrb.y, tLlf.z));
        vertices.push_back(cgt::vec3(urb.x, urb.y, llf.z));
        texCoords.push_back(cgt::vec3(tLlf.x, tUrb.y, tLlf.z));
        vertices.push_back(cgt::vec3(llf.x, urb.y, llf.z));
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<cgt::vec4>(), std::vector<cgt::vec3>(4, cgt::vec3(0.f, 1.f, 0.f))));
95
96
97
98
        vertices.clear();
        texCoords.clear();

        // left
99
100
101
102
103
104
105
106
107
        texCoords.push_back(cgt::vec3(tLlf.x, tUrb.y, tUrb.z));
        vertices.push_back(cgt::vec3(llf.x, urb.y, urb.z));
        texCoords.push_back(cgt::vec3(tLlf.x, tUrb.y, tLlf.z));
        vertices.push_back(cgt::vec3(llf.x, urb.y, llf.z));
        texCoords.push_back(cgt::vec3(tLlf.x, tLlf.y, tLlf.z));
        vertices.push_back(cgt::vec3(llf.x, llf.y, llf.z));
        texCoords.push_back(cgt::vec3(tLlf.x, tLlf.y, tUrb.z));
        vertices.push_back(cgt::vec3(llf.x, llf.y, urb.z));
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<cgt::vec4>(), std::vector<cgt::vec3>(4, cgt::vec3(-1.f, 0.f, 0.f))));
108
109
110
111
        vertices.clear();
        texCoords.clear();

        // bottom
112
113
114
115
116
117
118
119
120
        texCoords.push_back(cgt::vec3(tLlf.x, tLlf.y, tLlf.z));
        vertices.push_back(cgt::vec3(llf.x, llf.y, llf.z));
        texCoords.push_back(cgt::vec3(tUrb.x, tLlf.y, tLlf.z));
        vertices.push_back(cgt::vec3(urb.x, llf.y, llf.z));
        texCoords.push_back(cgt::vec3(tUrb.x, tLlf.y, tUrb.z));
        vertices.push_back(cgt::vec3(urb.x, llf.y, urb.z));
        texCoords.push_back(cgt::vec3(tLlf.x, tLlf.y, tUrb.z));
        vertices.push_back(cgt::vec3(llf.x, llf.y, urb.z));
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<cgt::vec4>(), std::vector<cgt::vec3>(4, cgt::vec3(0.f, -1.f, 0.f))));
121
122
123
124
        vertices.clear();
        texCoords.clear();

        // back
125
126
127
128
129
130
131
132
133
        texCoords.push_back(cgt::vec3(tUrb.x, tUrb.y, tUrb.z));
        vertices.push_back(cgt::vec3(urb.x, urb.y, urb.z));
        texCoords.push_back(cgt::vec3(tLlf.x, tUrb.y, tUrb.z));
        vertices.push_back(cgt::vec3(llf.x, urb.y, urb.z));
        texCoords.push_back(cgt::vec3(tLlf.x, tLlf.y, tUrb.z));
        vertices.push_back(cgt::vec3(llf.x, llf.y, urb.z));
        texCoords.push_back(cgt::vec3(tUrb.x, tLlf.y, tUrb.z));
        vertices.push_back(cgt::vec3(urb.x, llf.y, urb.z));
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<cgt::vec4>(), std::vector<cgt::vec3>(4, cgt::vec3(0.f, 0.f, 1.f))));
134
135
136
137
138
139
        vertices.clear();
        texCoords.clear();

        return new MeshGeometry(faces);
    }

140
    MultiIndexedGeometry* GeometryDataFactory::createTeapot() {
141
        std::vector<cgt::vec3> vertices, normals;
142
143
        vertices.reserve(Teapot::num_teapot_vertices);
        normals.reserve(Teapot::num_teapot_vertices);
144

145
        for (size_t i = 0; i < Teapot::num_teapot_vertices; ++i) {
146
147
            vertices.push_back(cgt::vec3(Teapot::teapot_vertices + 3*i));
            normals.push_back(cgt::vec3(Teapot::teapot_normals + 3*i));
148
149
        }

150
        MultiIndexedGeometry* toReturn = new MultiIndexedGeometry(vertices, std::vector<cgt::vec3>(), std::vector<cgt::vec4>(), normals);
151
152
153

        // convert indices and add primitives 
        int currentOffset = 0;
154
155
156
        while (currentOffset < Teapot::num_teapot_indices) {
            uint16_t count = Teapot::new_teapot_indicies[currentOffset];
            toReturn->addPrimitive(std::vector<uint16_t>(Teapot::new_teapot_indicies + currentOffset + 1, Teapot::new_teapot_indicies + count + currentOffset + 1));
157
158
159
160
161
162
            currentOffset += count + 1;
        }

        return toReturn;
    }

163
    MultiIndexedGeometry* GeometryDataFactory::createSphere(uint16_t numStacks /*= 6*/, uint16_t numSlices /*= 12*/, const cgt::vec3& exponents /*= cgt::vec3(1.f)*/) {
164
165
166
        cgtAssert(numStacks > 1 && numSlices > 2, "Sphere must have minimum 2 stacks and 3 slices!");
        std::vector<cgt::vec3> vertices;
        std::vector<cgt::vec3> textureCoordinates;
167
168

        // add top vertex
169
170
        vertices.push_back(cgt::vec3(0.f, 0.f, 1.f));
        textureCoordinates.push_back(cgt::vec3(0.f));
171
172
173

        // add middle vertices
        for (int i = 1; i < numStacks; ++i) {
174
            float phi = static_cast<float>(i) * cgt::PIf / static_cast<float>(numStacks);
175
176

            for (int j = 0; j < numSlices; ++j) {
177
                float theta = static_cast<float>(j) * 2.f*cgt::PIf / static_cast<float>(numSlices);
178
179
180
181
182
183
184
185
186
187
188

                // apply exponents for supersphere
                cgt::vec3 theVertex(cos(theta) * sin(phi), sin(theta) * sin(phi), cos(phi));
                for (size_t e = 0; e < 3; ++e) {
                    if (theVertex[e] < 0)
                        theVertex[e] = -pow(-theVertex[e], exponents[e]);
                    else
                        theVertex[e] = pow(theVertex[e], exponents[e]);
                }

                vertices.push_back(theVertex);
189
                textureCoordinates.push_back(cgt::vec3(theta / (2.f * cgt::PIf), phi / cgt::PIf, 0.f));
190
191
192
193
            }
        }

        // add bottom vertex
194
195
        vertices.push_back(cgt::vec3(0.f, 0.f, -1.f));
        textureCoordinates.push_back(cgt::vec3(1.f, 0.f, 0.f));
196
197

        // create geometry (in a unit sphere vertices = normals)
198
        MultiIndexedGeometry* toReturn = new MultiIndexedGeometry(vertices, textureCoordinates, std::vector<cgt::vec4>(), vertices);
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

        // add indices for primitives to geometry:
        {
            // top stack:
            std::vector<uint16_t> indices;
            for (uint16_t j = 0; j < numSlices; ++j) {
                indices.push_back(0);
                indices.push_back(j+1);
            }
            indices.push_back(0);
            indices.push_back(1);

            toReturn->addPrimitive(indices);
        }
        {
            // middle stacks:
            std::vector<uint16_t> indices;
            for (uint16_t i = 1; i < numStacks-1; ++i) {
                uint16_t startIndex = 1 + (i-1) * numSlices;

                for (uint16_t j = 0; j < numSlices; ++j) {
                    indices.push_back(startIndex + j);
                    indices.push_back(startIndex + numSlices + j);
                }
                indices.push_back(startIndex);
                indices.push_back(startIndex + numSlices);
            }

            toReturn->addPrimitive(indices);
        }
        {
            // bottom stack:
            std::vector<uint16_t> indices;
            uint16_t endIndex = static_cast<uint16_t>(vertices.size() - 1);

            for (uint16_t j = 0; j < numSlices; ++j) {
                indices.push_back(endIndex);
                indices.push_back(endIndex - (j+1));
            }
            indices.push_back(endIndex);
            indices.push_back(endIndex - 1);

            toReturn->addPrimitive(indices);
        }

        return toReturn;
    }

247
    MultiIndexedGeometry* GeometryDataFactory::createArrow(uint16_t numSlices, float tipLen, float cylRadius, float tipRadius) {
248
249
250
251
252
253
        cgtAssert(numSlices > 2, "Arrow shaft must have minimum 3 slices!");
        cgtAssert(tipRadius > cylRadius, "Tip radius must exceed cyclinder radius (for correct normals)!");
        cgtAssert(tipLen > 0, "Tip length must be between 0 and 1!");
        cgtAssert(tipLen < 1, "Tip length must be between 0 and 1!");
        std::vector<cgt::vec3> vertices;
        std::vector<cgt::vec3> normals;
254

255
        // add bottom vertex
256
257
        vertices.push_back(cgt::vec3(0.f, 0.f, 0.f));
        normals.push_back(cgt::vec3(0.f, 0.f, -1.f));
258

259
260
        // add shaft floor vertices
        for (int i = 0; i < numSlices; ++i) {
261
262
263
            float theta = static_cast<float>(i) * 2.f*cgt::PIf / static_cast<float>(numSlices);
            vertices.push_back(cgt::vec3(cylRadius * cos(theta), cylRadius * sin(theta), 0.f));
            normals.push_back(cgt::vec3(0.f, 0.f, -1.f));
264
265
        }
        for (int i = 0; i < numSlices; ++i) {
266
267
268
            float theta = static_cast<float>(i) * 2.f*cgt::PIf / static_cast<float>(numSlices);
            vertices.push_back(cgt::vec3(cylRadius * cos(theta), cylRadius * sin(theta), 0.f));
            normals.push_back(cgt::vec3(cos(theta), sin(theta), 0.f));
269
        }
270

271
272
        // add shaft top vertices
        for (int i = 0; i < numSlices; ++i) {
273
274
275
            float theta = static_cast<float>(i) * 2.f*cgt::PIf / static_cast<float>(numSlices);
            vertices.push_back(cgt::vec3(cylRadius * cos(theta), cylRadius * sin(theta), 1.f - tipLen));
            normals.push_back(cgt::vec3(cos(theta), sin(theta), 0.f));
276
277
        }
        for (int i = 0; i < numSlices; ++i) {
278
279
280
            float theta = static_cast<float>(i) * 2.f*cgt::PIf / static_cast<float>(numSlices);
            vertices.push_back(cgt::vec3(cylRadius * cos(theta), cylRadius * sin(theta), 1.f - tipLen));
            normals.push_back(cgt::vec3(0.f, 0.f, -1.f));
281
        }
282

283
284
        // add arrow tip outer cone vertices
        for (int i = 0; i < numSlices; ++i) {
285
286
287
            float theta = static_cast<float>(i) * 2.f*cgt::PIf / static_cast<float>(numSlices);
            vertices.push_back(cgt::vec3(tipRadius * cos(theta), tipRadius * sin(theta), 1.f - tipLen));
            normals.push_back(cgt::vec3(0.f, 0.f, -1.f));
288
289
290
        }
        float phi = atan2f(tipRadius, tipLen);
        for (int i = 0; i < numSlices; ++i) {
291
292
293
            float theta = static_cast<float>(i) * 2.f*cgt::PIf / static_cast<float>(numSlices);
            vertices.push_back(cgt::vec3(tipRadius * cos(theta), tipRadius * sin(theta), 1.f - tipLen));
            normals.push_back(cgt::vec3(cos(theta) * cos(phi), sin(theta) * cos(phi), sin(phi)));
294
        }
295

296
        // add top vertex
297
298
        vertices.push_back(cgt::vec3(0.f, 0.f, 1.f));
        normals.push_back(cgt::vec3(0.f, 0.f, 1.f));
299
300

        // create geometry
301
        MultiIndexedGeometry* toReturn = new MultiIndexedGeometry(vertices, std::vector<cgt::vec3>(), std::vector<cgt::vec4>(), normals);
302
303
304
305
306
307
308
309
310
311
312
313
314
315

        // add indices for primitives to geometry:
        {
            // cylinder floor
            std::vector<uint16_t> indices;
            for (uint16_t j = 0; j < numSlices; ++j) {
                indices.push_back(0);
                indices.push_back(j+1);
            }
            indices.push_back(0);
            indices.push_back(1);

            toReturn->addPrimitive(indices);
        }
316
        {
317
318
319
320
321
322
323
324
325
326
327
            // cylinder shaft
            std::vector<uint16_t> indices;
            for (uint16_t j = 0; j < numSlices; ++j) {
                indices.push_back(j+1+numSlices);
                indices.push_back(j+1+numSlices*2);
            }
            indices.push_back(1+numSlices);
            indices.push_back(1+numSlices*2);

            toReturn->addPrimitive(indices);
        }
328
        {
329
330
331
332
333
334
335
336
337
338
339
            // arrow tip bottom area
            std::vector<uint16_t> indices;
            for (uint16_t j = 0; j < numSlices; ++j) {
                indices.push_back(j+1+numSlices*3);
                indices.push_back(j+1+numSlices*4);
            }
            indices.push_back(1+numSlices*3);
            indices.push_back(1+numSlices*4);

            toReturn->addPrimitive(indices);
        }
340
        {
341
            // arrow tip cone
342
            uint16_t m = static_cast<uint16_t>(vertices.size() - 1);
343
344
345
            std::vector<uint16_t> indices;
            for (uint16_t j = 0; j < numSlices; ++j) {
                indices.push_back(j+1+numSlices*5);
346
                indices.push_back(m);
347
348
349
350
351
352
353
            }
            indices.push_back(1+numSlices*5);
            indices.push_back(m);

            toReturn->addPrimitive(indices);
        }

354
355
        return toReturn;
    }
356

357
}