/
Geometry.cpp
349 lines (298 loc) · 12.8 KB
/
Geometry.cpp
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
// ======================================================================== //
// Copyright 2019 Ingo Wald //
// //
// 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 //
// //
// 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. //
// ======================================================================== //
#include "SemanticParser.h"
// ply parser:
#include "../3rdParty/happly.h"
namespace pbrt {
namespace ply {
void parse(const std::string &fileName,
std::vector<vec3f> &pos,
std::vector<vec3f> &nor,
std::vector<vec2f> &tex,
std::vector<vec3i> &idx)
{
happly::PLYData ply(fileName);
if(ply.hasElement("vertex")) {
happly::Element& elem = ply.getElement("vertex");
if(elem.hasProperty("x") && elem.hasProperty("y") && elem.hasProperty("z")) {
std::vector<float> x = elem.getProperty<float>("x");
std::vector<float> y = elem.getProperty<float>("y");
std::vector<float> z = elem.getProperty<float>("z");
pos.resize(x.size());
for(int i = 0; i < (int)x.size(); i ++) {
pos[i] = vec3f(x[i], y[i], z[i]);
}
} else {
throw std::runtime_error("missing positions in ply");
}
if(elem.hasProperty("nx") && elem.hasProperty("ny") && elem.hasProperty("nz")) {
std::vector<float> x = elem.getProperty<float>("nx");
std::vector<float> y = elem.getProperty<float>("ny");
std::vector<float> z = elem.getProperty<float>("nz");
nor.resize(x.size());
for(int i = 0; i < (int)x.size(); i ++) {
nor[i] = vec3f(x[i], y[i], z[i]);
}
}
if(elem.hasProperty("u") && elem.hasProperty("v")) {
std::vector<float> u = elem.getProperty<float>("u");
std::vector<float> v = elem.getProperty<float>("v");
tex.resize(u.size());
for(int i = 0; i < (int)u.size(); i ++) {
tex[i] = vec2f(u[i], v[i]);
}
}
} else {
throw std::runtime_error("missing positions in ply");
}
if (ply.hasElement("face")) {
happly::Element& elem = ply.getElement("face");
if(elem.hasProperty("vertex_indices")) {
std::vector<std::vector<int>> faces = elem.getListPropertyAnySign<int>("vertex_indices");
for(int j = 0; j < (int)faces.size(); j ++) {
std::vector<int>& face = faces[j];
for (int i=2;i<(int)face.size();i++) {
idx.push_back(vec3i(face[0], face[i-1], face[i]));
}
}
} else {
throw std::runtime_error("missing faces in ply");
}
} else {
throw std::runtime_error("missing faces in ply");
}
}
} // ::pbrt::ply
Instance::SP SemanticParser::emitInstance(pbrt::syntactic::Object::Instance::SP pbrtInstance)
{
Instance::SP ourInstance
= std::make_shared<Instance>();
ourInstance->xfm = (const affine3f&)pbrtInstance->xfm.atStart;
ourInstance->object = findOrEmitObject(pbrtInstance->object);
return ourInstance;
}
Shape::SP SemanticParser::emitPlyMesh(pbrt::syntactic::Shape::SP shape)
{
const std::string fileName
= pbrtScene->makeGlobalFileName(shape->getParamString("filename"));
TriangleMesh::SP ours = std::make_shared<TriangleMesh>(findOrCreateMaterial(shape->material));
ply::parse(fileName,ours->vertex,ours->normal,ours->texcoord,ours->index);
affine3f xfm = shape->transform.atStart;
for (vec3f &v : ours->vertex)
v = xfmPoint(xfm,v);
for (vec3f &v : ours->normal)
v = xfmNormal(xfm,v);
extractTextures(ours,shape);
return ours;
}
Shape::SP SemanticParser::emitTriangleMesh(pbrt::syntactic::Shape::SP shape)
{
TriangleMesh::SP ours = std::make_shared<TriangleMesh>(findOrCreateMaterial(shape->material));
// vertices - param "P", 3x float each
ours->vertex = extractVector<vec3f>(shape,"P");
// vertex normals - param "N", 3x float each
ours->normal = extractVector<vec3f>(shape,"N");
// per-vertex texture coordinates - param "uv", 2x float each
ours->texcoord = extractVector<vec2f>(shape,"uv");
// triangle vertex indices - param "indices", 3x int each
ours->index = extractVector<vec3i>(shape,"indices");
affine3f xfm = shape->transform.atStart;
for (vec3f &v : ours->vertex)
v = xfmPoint(xfm,v);
for (vec3f &v : ours->normal)
v = xfmNormal(xfm,v);
extractTextures(ours,shape);
return ours;
}
Shape::SP SemanticParser::emitCurve(pbrt::syntactic::Shape::SP shape)
{
Curve::SP ours = std::make_shared<Curve>(findOrCreateMaterial(shape->material));
ours->transform = shape->transform.atStart;
// -------------------------------------------------------
// check 'type'
// -------------------------------------------------------
const std::string type
= shape->hasParamString("type")
? shape->getParamString("type")
: std::string("");
if (type == "cylinder")
ours->type = Curve::CurveType_Cylinder;
else if (type == "ribbon")
ours->type = Curve::CurveType_Ribbon;
else if (type == "flat")
ours->type = Curve::CurveType_Flat;
else
ours->type = Curve::CurveType_Unknown;
// -------------------------------------------------------
// check 'basis'
// -------------------------------------------------------
const std::string basis
= shape->hasParamString("basis")
? shape->getParamString("basis")
: std::string("");
if (basis == "bezier")
ours->basis = Curve::CurveBasis_Bezier;
else if (basis == "bspline")
ours->basis = Curve::CurveBasis_BSpline;
else
ours->basis = Curve::CurveBasis_Unknown;
// -------------------------------------------------------
// check 'width', 'width0', 'width1'
// -------------------------------------------------------
if (shape->hasParam1f("width"))
ours->width0 = ours->width1 = shape->getParam1f("width");
if (shape->hasParam1f("width0"))
ours->width0 = shape->getParam1f("width0");
if (shape->hasParam1f("width1"))
ours->width1 = shape->getParam1f("width1");
if (shape->hasParam1i("degree"))
ours->degree = shape->getParam1i("degree");
// -------------------------------------------------------
// vertices - param "P", 3x float each
// -------------------------------------------------------
ours->P = extractVector<vec3f>(shape,"P");
return ours;
}
Shape::SP SemanticParser::emitSphere(pbrt::syntactic::Shape::SP shape)
{
Sphere::SP ours = std::make_shared<Sphere>(findOrCreateMaterial(shape->material));
ours->transform = shape->transform.atStart;
ours->radius = shape->getParam1f("radius");
extractTextures(ours,shape);
return ours;
}
Shape::SP SemanticParser::emitDisk(pbrt::syntactic::Shape::SP shape)
{
Disk::SP ours = std::make_shared<Disk>(findOrCreateMaterial(shape->material));
ours->transform = shape->transform.atStart;
ours->radius = shape->getParam1f("radius");
if (shape->hasParam1f("height"))
ours->height = shape->getParam1f("height");
extractTextures(ours,shape);
return ours;
}
Shape::SP SemanticParser::emitShape(pbrt::syntactic::Shape::SP shape)
{
if (shape->type == "plymesh")
return emitPlyMesh(shape);
if (shape->type == "trianglemesh")
return emitTriangleMesh(shape);
if (shape->type == "curve")
return emitCurve(shape);
if (shape->type == "sphere")
return emitSphere(shape);
if (shape->type == "disk")
return emitDisk(shape);
// throw std::runtime_error("un-handled shape "+shape->type);
unhandledShapeTypeCounter[shape->type]++;
// std::cout << "WARNING: un-handled shape " << shape->type << std::endl;
return Shape::SP();
}
AreaLight::SP SemanticParser::parseAreaLight(pbrt::syntactic::AreaLightSource::SP in)
{
if (!in)
return AreaLight::SP();
if (in->type == "diffuse") {
/*! parsing here is a bit more tricky, because the L parameter
can be either an RGB, or a blackbody. Since the latter has
two floats to the former's three floats we'll simply switch
based on parameter count here .... */
if (in->hasParam2f("L")) {
DiffuseAreaLightBB::SP diffuse = std::make_shared<DiffuseAreaLightBB>();
vec2f v;
in->getParam2f(&v.x,"L");
diffuse->temperature = v.x;
diffuse->scale = v.y;
return diffuse;
} else if (in->hasParam3f("L")) {
DiffuseAreaLightRGB::SP diffuse = std::make_shared<DiffuseAreaLightRGB>();
in->getParam3f(&diffuse->L.x,"L");
return diffuse;
} else {
std::cout << "Warning: diffuse area light, but no 'L' parameter, or L is neither two (blackbody) nor three (rgba) floats?! Ignoring." << std::endl;
return AreaLight::SP();
}
// auto L_it = in->param.find("L");
// if (L_it == in->param.end()) {
// std::cout << "Warning: diffuse area light, but no 'L' parameter?! Ignoring." << std::endl;
// return AreaLight::SP();
// }
// syntactic::Param::SP param = L_it->second;
// if (param->getType() == "blackbody") {
// // not blackbody - assume it's RGB
// DiffuseAreaLightBB::SP diffuse = std::make_shared<DiffuseAreaLightBB>();
// in->getParam3f(&diffuse->blackBody.x,"L");
// return diffuse;
// } else {
// // not blackbody - assume it's RGB
// DiffuseAreaLightRGB::SP diffuse = std::make_shared<DiffuseAreaLightRGB>();
// in->getParam3f(&diffuse->blackBody.x,"L");
// return diffuse;
// }
}
std::cout << "Warning: unknown area light type '" << in->type << "'." << std::endl;
return AreaLight::SP();
}
/*! check if shapehas already been emitted, and return reference
is so; else emit new and return reference */
Shape::SP SemanticParser::findOrCreateShape(pbrt::syntactic::Shape::SP pbrtShape)
{
if (emittedShapes.find(pbrtShape) != emittedShapes.end())
return emittedShapes[pbrtShape];
Shape::SP newShape = emitShape(pbrtShape);
emittedShapes[pbrtShape] = newShape;
if (pbrtShape->attributes) {
newShape->reverseOrientation
= pbrtShape->attributes->reverseOrientation;
/* now, add area light sources */
if (!pbrtShape->attributes->areaLightSources.empty()) {
std::cout << "Shape has " << pbrtShape->attributes->areaLightSources.size()
<< " area light sources..." << std::endl;
auto &areaLights = pbrtShape->attributes->areaLightSources;
if (!areaLights.empty()) {
if (areaLights.size() > 1)
std::cout << "Warning: Shape has more than one area light!?" << std::endl;
newShape->areaLight = parseAreaLight(areaLights[0]);
}
}
}
return newShape;
}
/*! check if object has already been emitted, and return reference
is so; else emit new and return reference */
Object::SP SemanticParser::findOrEmitObject(pbrt::syntactic::Object::SP pbrtObject)
{
if (emittedObjects.find(pbrtObject) != emittedObjects.end()) {
return emittedObjects[pbrtObject];
}
Object::SP ourObject = std::make_shared<Object>();
emittedObjects[pbrtObject] = ourObject;
ourObject->name = pbrtObject->name;
for (auto lightSource : pbrtObject->lightSources) {
LightSource::SP ourLightSource = findOrCreateLightSource(lightSource);
if (ourLightSource)
ourObject->lightSources.push_back(ourLightSource);
}
for (auto shape : pbrtObject->shapes) {
Shape::SP ourShape = findOrCreateShape(shape);
if (ourShape)
ourObject->shapes.push_back(ourShape);
}
for (auto instance : pbrtObject->objectInstances)
ourObject->instances.push_back(emitInstance(instance));
return ourObject;
}
} // ::pbrt