iMSTK
Interactive Medical Simulation Toolkit
Sphere.cpp
1 /*=========================================================================
2 
3  Library: iMSTK
4 
5  Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
6  & Imaging in Medicine, Rensselaer Polytechnic Institute.
7 
8  Licensed under the Apache License, Version 2.0 (the "License",
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0.txt
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 =========================================================================*/
21 
22 #include "imstkMath.h"
23 #include "imstkVecDataArray.h"
24 
25 using namespace imstk;
26 
27 std::pair<std::shared_ptr<VecDataArray<double, 3>>, std::shared_ptr<VecDataArray<int, 3>>>
28 getSphere()
29 {
30  std::vector<double> buffVertices
31  {
32  0, 0, -0.5,
33  0, 0.262865603, -0.425325394,
34  0.249999985, 0.0812300518, -0.425325394,
35  0, 0.44721368, -0.223606572,
36  0.249999985, 0.344095677, -0.262865305,
37  0.425325423, 0.138196841, -0.223606601,
38  0.154508501, -0.212662712, -0.425325423,
39  0.404508621, -0.131432697, -0.262865394,
40  0.262865603, -0.361803472, -0.223606631,
41  -0.154508501, -0.212662712, -0.425325423,
42  0, -0.425325513, -0.262865365,
43  -0.262865603, -0.361803472, -0.223606631,
44  -0.249999985, 0.0812300518, -0.425325394,
45  -0.404508621, -0.131432697, -0.262865394,
46  -0.425325423, 0.138196841, -0.223606601,
47  -0.249999985, 0.344095677, -0.262865305,
48  -0.154508486, 0.4755283, 0,
49  -0.404508412, 0.293892711, 0,
50  -0.262865603, 0.361803472, 0.223606631,
51  -0.5, 0, 0,
52  -0.404508412, -0.293892711, 0,
53  -0.425325423, -0.138196841, 0.223606601,
54  -0.154508486, -0.4755283, 0,
55  0.154508486, -0.4755283, 0,
56  0, -0.44721368, 0.223606572,
57  0.404508412, -0.293892711, 0,
58  0.5, 0, 0,
59  0.425325423, -0.138196841, 0.223606601,
60  0.404508412, 0.293892711, 0,
61  0.154508486, 0.4755283, 0,
62  0.262865603, 0.361803472, 0.223606631,
63  0, 0.425325513, 0.262865365,
64  -0.404508621, 0.131432697, 0.262865394,
65  -0.249999985, -0.344095677, 0.262865305,
66  0.249999985, -0.344095677, 0.262865305,
67  0.404508621, 0.131432697, 0.262865394,
68  0, 0, 0.5,
69  0.154508501, 0.212662712, 0.425325423,
70  -0.154508501, 0.212662712, 0.425325423,
71  0.249999985, -0.0812300518, 0.425325394,
72  0, -0.262865603, 0.425325394,
73  -0.249999985, -0.0812300518, 0.425325394
74  };
75 
76  std::vector<int> buffFaces
77  {
78  3, 1, 2,
79  5, 2, 4,
80  6, 3, 5,
81  3, 2, 5,
82  7, 1, 3,
83  8, 3, 6,
84  9, 7, 8,
85  7, 3, 8,
86  10, 1, 7,
87  11, 7, 9,
88  12, 10, 11,
89  10, 7, 11,
90  13, 1, 10,
91  14, 10, 12,
92  15, 13, 14,
93  13, 10, 14,
94  2, 1, 13,
95  16, 13, 15,
96  4, 2, 16,
97  2, 13, 16,
98  17, 4, 16,
99  18, 16, 15,
100  19, 17, 18,
101  17, 16, 18,
102  20, 15, 14,
103  21, 14, 12,
104  22, 20, 21,
105  20, 14, 21,
106  23, 12, 11,
107  24, 11, 9,
108  25, 23, 24,
109  23, 11, 24,
110  26, 9, 8,
111  27, 8, 6,
112  28, 26, 27,
113  26, 8, 27,
114  29, 6, 5,
115  30, 5, 4,
116  31, 29, 30,
117  29, 5, 30,
118  30, 4, 17,
119  32, 17, 19,
120  31, 30, 32,
121  30, 17, 32,
122  18, 15, 20,
123  33, 20, 22,
124  19, 18, 33,
125  18, 20, 33,
126  21, 12, 23,
127  34, 23, 25,
128  22, 21, 34,
129  21, 23, 34,
130  24, 9, 26,
131  35, 26, 28,
132  25, 24, 35,
133  24, 26, 35,
134  27, 6, 29,
135  36, 29, 31,
136  28, 27, 36,
137  27, 29, 36,
138  39, 37, 38,
139  32, 38, 31,
140  19, 39, 32,
141  39, 38, 32,
142  38, 37, 40,
143  36, 40, 28,
144  31, 38, 36,
145  38, 40, 36,
146  40, 37, 41,
147  35, 41, 25,
148  28, 40, 35,
149  40, 41, 35,
150  41, 37, 42,
151  34, 42, 22,
152  25, 41, 34,
153  41, 42, 34,
154  42, 37, 39,
155  33, 39, 19,
156  22, 42, 33,
157  42, 39, 33
158  };
159 
160  std::shared_ptr<VecDataArray<double, 3>> verticesPtr = std::make_shared<VecDataArray<double, 3>>();
161  VecDataArray<double, 3>& vertices = *verticesPtr;
162  vertices.reserve(static_cast<int>(buffVertices.size() / 3));
163  for (size_t i = 0; i < buffVertices.size() / 3; ++i)
164  {
165  vertices.push_back(Vec3d(buffVertices[i * 3],
166  buffVertices[i * 3 + 1],
167  buffVertices[i * 3 + 2]));
168  }
169 
170  std::shared_ptr<VecDataArray<int, 3>> facesPtr = std::make_shared<VecDataArray<int, 3>>();
171  VecDataArray<int, 3>& faces = *facesPtr;
172  faces.reserve(static_cast<int>(buffFaces.size() / 3));
173  for (size_t i = 0; i < buffFaces.size() / 3; ++i)
174  {
175  // Face ID of triangles is 0-based index (data from .obj file is 1-based index)
176  faces.push_back(Vec3i(buffFaces[i * 3] - 1, buffFaces[i * 3 + 1] - 1, buffFaces[i * 3 + 2] - 1));
177  }
178 
179  return { verticesPtr, facesPtr };
180 }
Compound Geometry.
void reserve(const int size) override
Allocates extra capacity, for the number of values, conservative reallocate.