16.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
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
namespace campvis {

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

        vertices.push_back(tgt::vec3(llf.x, llf.y, llf.z));
        vertices.push_back(tgt::vec3(urb.x, llf.y, llf.z));
        vertices.push_back(tgt::vec3(urb.x, urb.y, llf.z));
        vertices.push_back(tgt::vec3(llf.x, urb.y, llf.z));

        texCorods.push_back(tgt::vec3(texLlf.x, texLlf.y, texLlf.z));
        texCorods.push_back(tgt::vec3(texUrb.x, texLlf.y, texLlf.z));
        texCorods.push_back(tgt::vec3(texUrb.x, texUrb.y, texLlf.z));
        texCorods.push_back(tgt::vec3(texLlf.x, texUrb.y, texLlf.z));

        return new FaceGeometry(vertices, texCorods);
    }

    MeshGeometry* GeometryDataFactory::createCube(const tgt::Bounds& bounds, const tgt::Bounds& texBounds) {
        const tgt::vec3& llf = bounds.getLLF();
        const tgt::vec3& urb = bounds.getURB();
        const tgt::vec3& tLlf = texBounds.getLLF();
        const tgt::vec3& tUrb = texBounds.getURB();

        // not the most elegant method, but it works...
        std::vector<tgt::vec3> vertices, texCoords;
        std::vector<FaceGeometry> faces;

        // front
        texCoords.push_back(tgt::vec3(tLlf.x, tUrb.y, tLlf.z));
        vertices.push_back(tgt::vec3(llf.x, urb.y, llf.z));
        texCoords.push_back(tgt::vec3(tUrb.x, tUrb.y, tLlf.z));
        vertices.push_back(tgt::vec3(urb.x, urb.y, llf.z));
        texCoords.push_back(tgt::vec3(tUrb.x, tLlf.y, tLlf.z));
        vertices.push_back(tgt::vec3(urb.x, llf.y, llf.z));
        texCoords.push_back(tgt::vec3(tLlf.x, tLlf.y, tLlf.z));
        vertices.push_back(tgt::vec3(llf.x, llf.y, llf.z));
68
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<tgt::vec4>(), std::vector<tgt::vec3>(4, tgt::vec3(0.f, 0.f, -1.f))));
69
70
71
72
73
74
75
76
77
78
79
80
        vertices.clear();
        texCoords.clear();

        // right
        texCoords.push_back(tgt::vec3(tUrb.x, tUrb.y, tLlf.z));
        vertices.push_back(tgt::vec3(urb.x, urb.y, llf.z));
        texCoords.push_back(tgt::vec3(tUrb.x, tUrb.y, tUrb.z));
        vertices.push_back(tgt::vec3(urb.x, urb.y, urb.z));
        texCoords.push_back(tgt::vec3(tUrb.x, tLlf.y, tUrb.z));
        vertices.push_back(tgt::vec3(urb.x, llf.y, urb.z));
        texCoords.push_back(tgt::vec3(tUrb.x, tLlf.y, tLlf.z));
        vertices.push_back(tgt::vec3(urb.x, llf.y, llf.z));
81
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<tgt::vec4>(), std::vector<tgt::vec3>(4, tgt::vec3(1.f, 0.f, 0.f))));
82
83
84
85
86
87
88
89
90
91
92
93
        vertices.clear();
        texCoords.clear();

        // top
        texCoords.push_back(tgt::vec3(tLlf.x, tUrb.y, tUrb.z));
        vertices.push_back(tgt::vec3(llf.x, urb.y, urb.z));
        texCoords.push_back(tgt::vec3(tUrb.x, tUrb.y, tUrb.z));
        vertices.push_back(tgt::vec3(urb.x, urb.y, urb.z));
        texCoords.push_back(tgt::vec3(tUrb.x, tUrb.y, tLlf.z));
        vertices.push_back(tgt::vec3(urb.x, urb.y, llf.z));
        texCoords.push_back(tgt::vec3(tLlf.x, tUrb.y, tLlf.z));
        vertices.push_back(tgt::vec3(llf.x, urb.y, llf.z));
94
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<tgt::vec4>(), std::vector<tgt::vec3>(4, tgt::vec3(0.f, 1.f, 0.f))));
95
96
97
98
99
100
101
102
103
104
105
106
        vertices.clear();
        texCoords.clear();

        // left
        texCoords.push_back(tgt::vec3(tLlf.x, tUrb.y, tUrb.z));
        vertices.push_back(tgt::vec3(llf.x, urb.y, urb.z));
        texCoords.push_back(tgt::vec3(tLlf.x, tUrb.y, tLlf.z));
        vertices.push_back(tgt::vec3(llf.x, urb.y, llf.z));
        texCoords.push_back(tgt::vec3(tLlf.x, tLlf.y, tLlf.z));
        vertices.push_back(tgt::vec3(llf.x, llf.y, llf.z));
        texCoords.push_back(tgt::vec3(tLlf.x, tLlf.y, tUrb.z));
        vertices.push_back(tgt::vec3(llf.x, llf.y, urb.z));
107
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<tgt::vec4>(), std::vector<tgt::vec3>(4, tgt::vec3(-1.f, 0.f, 0.f))));
108
109
110
111
112
113
114
115
116
117
118
119
        vertices.clear();
        texCoords.clear();

        // bottom
        texCoords.push_back(tgt::vec3(tLlf.x, tLlf.y, tLlf.z));
        vertices.push_back(tgt::vec3(llf.x, llf.y, llf.z));
        texCoords.push_back(tgt::vec3(tUrb.x, tLlf.y, tLlf.z));
        vertices.push_back(tgt::vec3(urb.x, llf.y, llf.z));
        texCoords.push_back(tgt::vec3(tUrb.x, tLlf.y, tUrb.z));
        vertices.push_back(tgt::vec3(urb.x, llf.y, urb.z));
        texCoords.push_back(tgt::vec3(tLlf.x, tLlf.y, tUrb.z));
        vertices.push_back(tgt::vec3(llf.x, llf.y, urb.z));
120
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<tgt::vec4>(), std::vector<tgt::vec3>(4, tgt::vec3(0.f, -1.f, 0.f))));
121
122
123
124
125
126
127
128
129
130
131
132
        vertices.clear();
        texCoords.clear();

        // back
        texCoords.push_back(tgt::vec3(tUrb.x, tUrb.y, tUrb.z));
        vertices.push_back(tgt::vec3(urb.x, urb.y, urb.z));
        texCoords.push_back(tgt::vec3(tLlf.x, tUrb.y, tUrb.z));
        vertices.push_back(tgt::vec3(llf.x, urb.y, urb.z));
        texCoords.push_back(tgt::vec3(tLlf.x, tLlf.y, tUrb.z));
        vertices.push_back(tgt::vec3(llf.x, llf.y, urb.z));
        texCoords.push_back(tgt::vec3(tUrb.x, tLlf.y, tUrb.z));
        vertices.push_back(tgt::vec3(urb.x, llf.y, urb.z));
133
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<tgt::vec4>(), std::vector<tgt::vec3>(4, tgt::vec3(0.f, 0.f, 1.f))));
134
135
136
137
138
139
        vertices.clear();
        texCoords.clear();

        return new MeshGeometry(faces);
    }

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

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

        MultiIndexedGeometry* toReturn = new MultiIndexedGeometry(vertices, std::vector<tgt::vec3>(), std::vector<tgt::vec4>(), normals);

        // 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
164
165
    MultiIndexedGeometry* GeometryDataFactory::createSphere(uint16_t numStacks /*= 6*/, uint16_t numSlices /*= 12*/) {
        tgtAssert(numStacks > 1 && numSlices > 2, "Sphere must have minimum 2 stacks and 3 slices!");
        std::vector<tgt::vec3> vertices;
166
        std::vector<tgt::vec3> textureCoordinates;
167
168
169

        // add top vertex
        vertices.push_back(tgt::vec3(0.f, 0.f, 1.f));
170
        textureCoordinates.push_back(tgt::vec3(0.f));
171
172
173
174
175
176
177
178

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

            for (int j = 0; j < numSlices; ++j) {
                float theta = static_cast<float>(j) * 2.f*tgt::PIf / static_cast<float>(numSlices);
                vertices.push_back(tgt::vec3(cos(theta) * sin(phi), sin(theta) * sin(phi), cos(phi)));
179
                textureCoordinates.push_back(tgt::vec3(theta / (2.f * tgt::PIf), phi / tgt::PIf, 0.f));
180
181
182
183
184
            }
        }

        // add bottom vertex
        vertices.push_back(tgt::vec3(0.f, 0.f, -1.f));
185
        textureCoordinates.push_back(tgt::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<tgt::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
        tgtAssert(numSlices > 2, "Arrow shaft must have minimum 3 slices!");
239
240
241
        tgtAssert(tipRadius > cylRadius, "Tip radius must exceed cyclinder radius (for correct normals)!");
        tgtAssert(tipLen > 0, "Tip length must be between 0 and 1!");
        tgtAssert(tipLen < 1, "Tip length must be between 0 and 1!");
242
        std::vector<tgt::vec3> vertices;
243
        std::vector<tgt::vec3> normals;
244

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

249
250
        // add shaft floor vertices
        for (int i = 0; i < numSlices; ++i) {
251
252
            float theta = static_cast<float>(i) * 2.f*tgt::PIf / static_cast<float>(numSlices);
            vertices.push_back(tgt::vec3(cylRadius * cos(theta), cylRadius * sin(theta), 0.f));
253
254
255
            normals.push_back(tgt::vec3(0.f, 0.f, -1.f));
        }
        for (int i = 0; i < numSlices; ++i) {
256
257
            float theta = static_cast<float>(i) * 2.f*tgt::PIf / static_cast<float>(numSlices);
            vertices.push_back(tgt::vec3(cylRadius * cos(theta), cylRadius * sin(theta), 0.f));
258
259
            normals.push_back(tgt::vec3(cos(theta), sin(theta), 0.f));
        }
260

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

273
274
        // add arrow tip outer cone vertices
        for (int i = 0; i < numSlices; ++i) {
275
276
            float theta = static_cast<float>(i) * 2.f*tgt::PIf / static_cast<float>(numSlices);
            vertices.push_back(tgt::vec3(tipRadius * cos(theta), tipRadius * sin(theta), 1.f - tipLen));
277
278
279
280
            normals.push_back(tgt::vec3(0.f, 0.f, -1.f));
        }
        float phi = atan2f(tipRadius, tipLen);
        for (int i = 0; i < numSlices; ++i) {
281
282
            float theta = static_cast<float>(i) * 2.f*tgt::PIf / static_cast<float>(numSlices);
            vertices.push_back(tgt::vec3(tipRadius * cos(theta), tipRadius * sin(theta), 1.f - tipLen));
283
284
            normals.push_back(tgt::vec3(cos(theta) * cos(phi), sin(theta) * cos(phi), sin(phi)));
        }
285

286
        // add top vertex
287
        vertices.push_back(tgt::vec3(0.f, 0.f, 1.f));
288
        normals.push_back(tgt::vec3(0.f, 0.f, 1.f));
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305

        // create geometry
        MultiIndexedGeometry* toReturn = new MultiIndexedGeometry(vertices, std::vector<tgt::vec3>(), std::vector<tgt::vec4>(), normals);

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