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.1 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*/) {
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
178
179
                float theta = static_cast<float>(j) * 2.f*cgt::PIf / static_cast<float>(numSlices);
                vertices.push_back(cgt::vec3(cos(theta) * sin(phi), sin(theta) * sin(phi), cos(phi)));
                textureCoordinates.push_back(cgt::vec3(theta / (2.f * cgt::PIf), phi / cgt::PIf, 0.f));
180
181
182
183
            }
        }

        // add bottom vertex
184
185
        vertices.push_back(cgt::vec3(0.f, 0.f, -1.f));
        textureCoordinates.push_back(cgt::vec3(1.f, 0.f, 0.f));
186
187

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

        // 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;
    }

237
    MultiIndexedGeometry* GeometryDataFactory::createArrow(uint16_t numSlices, float tipLen, float cylRadius, float tipRadius) {
238
239
240
241
242
243
        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;
244

245
        // add bottom vertex
246
247
        vertices.push_back(cgt::vec3(0.f, 0.f, 0.f));
        normals.push_back(cgt::vec3(0.f, 0.f, -1.f));
248

249
250
        // add shaft floor vertices
        for (int i = 0; i < numSlices; ++i) {
251
252
253
            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));
254
255
        }
        for (int i = 0; i < numSlices; ++i) {
256
257
258
            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));
259
        }
260

261
262
        // add shaft top vertices
        for (int i = 0; i < numSlices; ++i) {
263
264
265
            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));
266
267
        }
        for (int i = 0; i < numSlices; ++i) {
268
269
270
            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));
271
        }
272

273
274
        // add arrow tip outer cone vertices
        for (int i = 0; i < numSlices; ++i) {
275
276
277
            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));
278
279
280
        }
        float phi = atan2f(tipRadius, tipLen);
        for (int i = 0; i < numSlices; ++i) {
281
282
283
            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)));
284
        }
285

286
        // add top vertex
287
288
        vertices.push_back(cgt::vec3(0.f, 0.f, 1.f));
        normals.push_back(cgt::vec3(0.f, 0.f, 1.f));
289
290

        // create geometry
291
        MultiIndexedGeometry* toReturn = new MultiIndexedGeometry(vertices, std::vector<cgt::vec3>(), std::vector<cgt::vec4>(), normals);
292
293
294
295
296
297
298
299
300
301
302
303
304
305

        // 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);
        }
306
        {
307
308
309
310
311
312
313
314
315
316
317
            // 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);
        }
318
        {
319
320
321
322
323
324
325
326
327
328
329
            // 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);
        }
330
        {
331
            // arrow tip cone
332
            uint16_t m = static_cast<uint16_t>(vertices.size() - 1);
333
334
335
            std::vector<uint16_t> indices;
            for (uint16_t j = 0; j < numSlices; ++j) {
                indices.push_back(j+1+numSlices*5);
336
                indices.push_back(m);
337
338
339
340
341
342
343
            }
            indices.push_back(1+numSlices*5);
            indices.push_back(m);

            toReturn->addPrimitive(indices);
        }

344
345
        return toReturn;
    }
346

347
}