Blender V5.3
BlenderFileLoader.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BlenderFileLoader.h"
10
11#include "DNA_layer_types.h"
12#include "DNA_meshdata_types.h"
13
14#include "BLI_math_geom_c.hh"
15#include "BLI_math_matrix_c.hh"
16#include "BLI_math_vector_c.hh"
17#include "BLI_utildefines.hh"
18
19#include "BKE_attribute.hh"
20#include "BKE_customdata.hh"
21#include "BKE_global.hh"
22#include "BKE_mesh.hh"
23#include "BKE_object.hh"
24
25#include <sstream>
26
27using blender::float3;
28using blender::Span;
29
30namespace Freestyle {
31
33 blender::ViewLayer *view_layer,
34 blender::Depsgraph *depsgraph)
35{
36 _re = re;
37 _depsgraph = depsgraph;
38 _Scene = nullptr;
39 _numFacesRead = 0;
40#if 0
41 _minEdgeSize = DBL_MAX;
42#endif
44 _pRenderMonitor = nullptr;
45}
46
51
53{
55 cout << "\n=== Importing triangular meshes into Blender ===" << endl;
56 }
57
58 // creation of the scene root node
59 _Scene = new NodeGroup;
60
61 if (_re->clip_start < 0.0f) {
62 // Adjust clipping start/end and set up a Z offset when the viewport preview
63 // is used with the orthographic view. In this case, _re->clip_start is negative,
64 // while Freestyle assumes that imported mesh data are in the camera coordinate
65 // system with the view point located at origin [bug #36009].
66 _z_near = -0.001f;
69 }
70 else {
73 _z_offset = 0.0f;
74 }
75
76 int id = 0;
78
81 using blender::Object;
82
83 blender::DEGObjectIterSettings deg_iter_settings{};
84 deg_iter_settings.depsgraph = _depsgraph;
89
90 DEG_OBJECT_ITER_BEGIN (&deg_iter_settings, ob) {
92 break;
93 }
94
95 if ((ob->base_flag & (blender::BASE_HOLDOUT | blender::BASE_INDIRECT_ONLY)) ||
96 (ob->visibility_flag & blender::OB_HOLDOUT))
97 {
98 continue;
99 }
100
101 if (!(BKE_object_visibility(ob, eval_mode) & blender::OB_VISIBLE_SELF)) {
102 continue;
103 }
104
105 /* Evaluated meta-balls will appear as mesh objects in the iterator. */
106 if (ob->type == blender::OB_MBALL) {
107 continue;
108 }
109
110 blender::Mesh *mesh = BKE_object_to_mesh(nullptr, ob, false);
111
112 if (mesh) {
113 insertShapeNode(ob, mesh, ++id);
114 BKE_object_to_mesh_clear(ob);
115 }
116 }
118
119 // Return the built scene.
120 return _Scene;
121}
122
123#define CLIPPED_BY_NEAR -1
124#define NOT_CLIPPED 0
125#define CLIPPED_BY_FAR 1
126
127// check if each vertex of a triangle (V1, V2, V3) is clipped by the near/far plane
128// and calculate the number of triangles to be generated by clipping
129int BlenderFileLoader::countClippedFaces(float v1[3], float v2[3], float v3[3], int clip[3])
130{
131 float *v[3];
132 int numClipped, sum, numTris = 0;
133
134 v[0] = v1;
135 v[1] = v2;
136 v[2] = v3;
137 numClipped = sum = 0;
138 for (int i = 0; i < 3; i++) {
139 if (v[i][2] > _z_near) {
140 clip[i] = CLIPPED_BY_NEAR;
141 numClipped++;
142 }
143 else if (v[i][2] < _z_far) {
144 clip[i] = CLIPPED_BY_FAR;
145 numClipped++;
146 }
147 else {
148 clip[i] = NOT_CLIPPED;
149 }
150#if 0
152 printf("%d %s\n",
153 i,
154 (clip[i] == NOT_CLIPPED) ? "not" :
155 (clip[i] == CLIPPED_BY_NEAR) ? "near" :
156 "far");
157 }
158#endif
159 sum += clip[i];
160 }
161 switch (numClipped) {
162 case 0:
163 numTris = 1; // triangle
164 break;
165 case 1:
166 numTris = 2; // tetragon
167 break;
168 case 2:
169 if (sum == 0) {
170 numTris = 3; // pentagon
171 }
172 else {
173 numTris = 1; // triangle
174 }
175 break;
176 case 3:
177 if (ELEM(sum, 3, -3)) {
178 numTris = 0;
179 }
180 else {
181 numTris = 2; // tetragon
182 }
183 break;
184 }
185 return numTris;
186}
187
188// find the intersection point C between the line segment from V1 to V2 and
189// a clipping plane at depth Z (i.e., the Z component of C is known, while
190// the X and Y components are unknown).
191void BlenderFileLoader::clipLine(float v1[3], float v2[3], float c[3], float z)
192{
193 // Order v1 and v2 by Z values to make sure that clipLine(P, Q, c, z)
194 // and clipLine(Q, P, c, z) gives exactly the same numerical result.
195 float *p, *q;
196 if (v1[2] < v2[2]) {
197 p = v1;
198 q = v2;
199 }
200 else {
201 p = v2;
202 q = v1;
203 }
204 double d[3];
205 for (int i = 0; i < 3; i++) {
206 d[i] = q[i] - p[i];
207 }
208 double t = (z - p[2]) / d[2];
209 c[0] = p[0] + t * d[0];
210 c[1] = p[1] + t * d[1];
211 c[2] = z;
212}
213
214// clip the triangle (V1, V2, V3) by the near and far clipping plane and
215// obtain a set of vertices after the clipping. The number of vertices
216// is at most 5.
218 float triCoords[][3],
219 float v1[3],
220 float v2[3],
221 float v3[3],
222 float triNormals[][3],
223 float n1[3],
224 float n2[3],
225 float n3[3],
226 bool edgeMarks[5],
227 bool em1,
228 bool em2,
229 bool em3,
230 const int clip[3])
231{
232 float *v[3], *n[3];
233 bool em[3];
234 int i, j, k;
235
236 v[0] = v1;
237 n[0] = n1;
238 v[1] = v2;
239 n[1] = n2;
240 v[2] = v3;
241 n[2] = n3;
242 em[0] = em1; /* edge mark of the edge between v1 and v2 */
243 em[1] = em2; /* edge mark of the edge between v2 and v3 */
244 em[2] = em3; /* edge mark of the edge between v3 and v1 */
245 k = 0;
246 for (i = 0; i < 3; i++) {
247 j = (i + 1) % 3;
248 if (clip[i] == NOT_CLIPPED) {
249 blender::copy_v3_v3(triCoords[k], v[i]);
250 blender::copy_v3_v3(triNormals[k], n[i]);
251 edgeMarks[k] = em[i];
252 k++;
253 if (clip[j] != NOT_CLIPPED) {
254 clipLine(v[i], v[j], triCoords[k], (clip[j] == CLIPPED_BY_NEAR) ? _z_near : _z_far);
255 blender::copy_v3_v3(triNormals[k], n[j]);
256 edgeMarks[k] = false;
257 k++;
258 }
259 }
260 else if (clip[i] != clip[j]) {
261 if (clip[j] == NOT_CLIPPED) {
262 clipLine(v[i], v[j], triCoords[k], (clip[i] == CLIPPED_BY_NEAR) ? _z_near : _z_far);
263 blender::copy_v3_v3(triNormals[k], n[i]);
264 edgeMarks[k] = em[i];
265 k++;
266 }
267 else {
268 clipLine(v[i], v[j], triCoords[k], (clip[i] == CLIPPED_BY_NEAR) ? _z_near : _z_far);
269 blender::copy_v3_v3(triNormals[k], n[i]);
270 edgeMarks[k] = em[i];
271 k++;
272 clipLine(v[i], v[j], triCoords[k], (clip[j] == CLIPPED_BY_NEAR) ? _z_near : _z_far);
273 blender::copy_v3_v3(triNormals[k], n[j]);
274 edgeMarks[k] = false;
275 k++;
276 }
277 }
278 }
279 BLI_assert(k == 2 + numTris);
280 (void)numTris; /* Ignored in release builds. */
281}
282
284 float v1[3],
285 float v2[3],
286 float v3[3],
287 float n1[3],
288 float n2[3],
289 float n3[3],
290 bool fm,
291 bool em1,
292 bool em2,
293 bool em3)
294{
295 float *fv[3], *fn[3];
296#if 0
297 float len;
298#endif
299 uint i, j;
301
302 // initialize the bounding box by the first vertex
303 if (ls->currentIndex == 0) {
306 }
307
308 fv[0] = v1;
309 fn[0] = n1;
310 fv[1] = v2;
311 fn[1] = n2;
312 fv[2] = v3;
313 fn[2] = n3;
314 for (i = 0; i < 3; i++) {
315 blender::copy_v3_v3(ls->pv, fv[i]);
316 blender::copy_v3_v3(ls->pn, fn[i]);
317
318 // update the bounding box
319 for (j = 0; j < 3; j++) {
320 if (ls->minBBox[j] > ls->pv[j]) {
321 ls->minBBox[j] = ls->pv[j];
322 }
323
324 if (ls->maxBBox[j] < ls->pv[j]) {
325 ls->maxBBox[j] = ls->pv[j];
326 }
327 }
328
329#if 0
330 len = len_v3v3(fv[i], fv[(i + 1) % 3]);
331 if (_minEdgeSize > len) {
332 _minEdgeSize = len;
333 }
334#endif
335
336 *ls->pvi = ls->currentIndex;
337 *ls->pni = ls->currentIndex;
338 *ls->pmi = ls->currentMIndex;
339
340 ls->currentIndex += 3;
341 ls->pv += 3;
342 ls->pn += 3;
343
344 ls->pvi++;
345 ls->pni++;
346 ls->pmi++;
347 }
348
349 if (fm) {
351 }
352 if (em1) {
354 }
355 if (em2) {
357 }
358 if (em3) {
360 }
361 *(ls->pm++) = marks;
362}
363
364// With A, B and P indicating the three vertices of a given triangle, returns:
365// 1 if points A and B are in the same position in the 3D space;
366// 2 if the distance between point P and line segment AB is zero; and
367// zero otherwise.
368int BlenderFileLoader::testDegenerateTriangle(float v1[3], float v2[3], float v3[3])
369{
370 const float eps = 1.0e-6;
371 const float eps_sq = eps * eps;
372
373#if 0
374 float area = area_tri_v3(v1, v2, v3);
375 bool verbose = (area < 1.0e-6);
376#endif
377
378 if (blender::equals_v3v3(v1, v2) || blender::equals_v3v3(v2, v3) || blender::equals_v3v3(v1, v3))
379 {
380#if 0
381 if (verbose && G.debug & blender::G_DEBUG_FREESTYLE) {
382 printf("BlenderFileLoader::testDegenerateTriangle = 1\n");
383 }
384#endif
385 return 1;
386 }
387 if (blender::dist_squared_to_line_segment_v3(v1, v2, v3) < eps_sq ||
388 blender::dist_squared_to_line_segment_v3(v2, v1, v3) < eps_sq ||
389 blender::dist_squared_to_line_segment_v3(v3, v1, v2) < eps_sq)
390 {
391#if 0
392 if (verbose && G.debug & blender::G_DEBUG_FREESTYLE) {
393 printf("BlenderFileLoader::testDegenerateTriangle = 2\n");
394 }
395#endif
396 return 2;
397 }
398#if 0
399 if (verbose && G.debug & blender::G_DEBUG_FREESTYLE) {
400 printf("BlenderFileLoader::testDegenerateTriangle = 0\n");
401 }
402#endif
403 return 0;
404}
405
406static bool testEdgeMark(blender::Mesh *mesh,
407 const blender::VArray<bool> &fed,
408 const blender::int3 &tri,
409 int i)
410{
411 const Span<blender::int2> edges = mesh->edges();
412 const Span<int> corner_verts = mesh->corner_verts();
413 const Span<int> corner_edges = mesh->corner_edges();
414
415 const int corner = tri[i];
416 const int corner_next = tri[(i + 1) % 3];
417 const blender::int2 &edge = edges[corner_edges[corner]];
418
419 if (!ELEM(corner_verts[corner_next], edge[0], edge[1])) {
420 /* Not an edge in the original mesh before triangulation. */
421 return false;
422 }
423
424 return fed[corner_edges[corner]];
425}
426
428{
429 using namespace blender;
430 char *name = ob->id.name + 2;
431
432 const Span<float3> vert_positions = mesh->vert_positions();
433 const OffsetIndices mesh_polys = mesh->faces();
434 const Span<int> corner_verts = mesh->corner_verts();
435
436 // Compute loop triangles
437 int tottri = poly_to_tri_count(mesh->faces_num, mesh->corners_num);
438 blender::int3 *corner_tris = MEM_new_array_uninitialized<blender::int3>(size_t(tottri),
439 __func__);
441 vert_positions, mesh_polys, corner_verts, {corner_tris, tottri});
442 const Span<int> tri_faces = mesh->corner_tri_faces();
443 const Span<blender::float3> corner_normals = mesh->corner_normals();
444 const bke::AttributeAccessor attributes = mesh->attributes();
445
446 // Get other mesh data
447 const VArray<bool> fed = *attributes.lookup_or_default<bool>(
448 "freestyle_edge", bke::AttrDomain::Edge, false);
449 const VArray<bool> ffa = *attributes.lookup_or_default<bool>(
450 "freestyle_face", bke::AttrDomain::Face, false);
451
452 // Compute view matrix
454 float viewinv[4][4], viewmat[4][4];
455 RE_GetCameraModelMatrix(_re, ob_camera_eval, viewinv);
456 invert_m4_m4(viewmat, viewinv);
457
458 // Compute matrix including camera transform
459 float obmat[4][4], nmat[4][4];
460 mul_m4_m4m4(obmat, viewmat, ob->object_to_world().ptr());
461 invert_m4_m4(nmat, obmat);
462 transpose_m4(nmat);
463
464 // We count the number of triangles after the clipping by the near and far view
465 // planes is applied (NOTE: mesh vertices are in the camera coordinate system).
466 uint numFaces = 0;
467 float v1[3], v2[3], v3[3];
468 float n1[3], n2[3], n3[3], facenormal[3];
469 int clip[3];
470 for (int a = 0; a < tottri; a++) {
471 const int3 &tri = corner_tris[a];
472
473 copy_v3_v3(v1, vert_positions[corner_verts[tri[0]]]);
474 copy_v3_v3(v2, vert_positions[corner_verts[tri[1]]]);
475 copy_v3_v3(v3, vert_positions[corner_verts[tri[2]]]);
476
477 mul_m4_v3(obmat, v1);
478 mul_m4_v3(obmat, v2);
479 mul_m4_v3(obmat, v3);
480
481 v1[2] += _z_offset;
482 v2[2] += _z_offset;
483 v3[2] += _z_offset;
484
485 numFaces += countClippedFaces(v1, v2, v3, clip);
486 }
487#if 0
489 cout << "numFaces " << numFaces << endl;
490 }
491#endif
492 if (numFaces == 0) {
493 MEM_delete(corner_tris);
494 return;
495 }
496
497 // We allocate memory for the meshes to be imported
498 NodeGroup *currentMesh = new NodeGroup;
499 NodeShape *shape = new NodeShape;
500
501 uint vSize = 3 * 3 * numFaces;
502 float *vertices = new float[vSize];
503 uint nSize = vSize;
504 float *normals = new float[nSize];
505 uint *numVertexPerFaces = new uint[numFaces];
506 vector<Material *> meshMaterials;
507 vector<FrsMaterial> meshFrsMaterials;
508
510 uint i;
511 for (i = 0; i < numFaces; i++) {
512 faceStyle[i] = IndexedFaceSet::TRIANGLES;
513 numVertexPerFaces[i] = 3;
514 }
515
516 IndexedFaceSet::FaceEdgeMark *faceEdgeMarks = new IndexedFaceSet::FaceEdgeMark[numFaces];
517
518 uint viSize = 3 * numFaces;
519 uint *VIndices = new uint[viSize];
520 uint niSize = viSize;
521 uint *NIndices = new uint[niSize];
522 uint *MIndices = new uint[viSize]; // Material Indices
523
524 LoaderState ls;
525 ls.pv = vertices;
526 ls.pn = normals;
527 ls.pm = faceEdgeMarks;
528 ls.pvi = VIndices;
529 ls.pni = NIndices;
530 ls.pmi = MIndices;
531 ls.currentIndex = 0;
532 ls.currentMIndex = 0;
533
534 FrsMaterial tmpMat;
535
536 const VArray<int> material_indices = *attributes.lookup_or_default<int>(
537 "material_index", bke::AttrDomain::Face, 0);
538 const VArray<bool> sharp_faces = *attributes.lookup_or_default<bool>(
539 "sharp_face", bke::AttrDomain::Face, false);
540
541 // We parse the vlak nodes again and import meshes while applying the clipping
542 // by the near and far view planes.
543 for (int a = 0; a < tottri; a++) {
544 const int3 &tri = corner_tris[a];
545 const int poly_i = tri_faces[a];
546 Material *mat = BKE_object_material_get(ob, material_indices[poly_i] + 1);
547
548 copy_v3_v3(v1, vert_positions[corner_verts[tri[0]]]);
549 copy_v3_v3(v2, vert_positions[corner_verts[tri[1]]]);
550 copy_v3_v3(v3, vert_positions[corner_verts[tri[2]]]);
551
552 mul_m4_v3(obmat, v1);
553 mul_m4_v3(obmat, v2);
554 mul_m4_v3(obmat, v3);
555
556 v1[2] += _z_offset;
557 v2[2] += _z_offset;
558 v3[2] += _z_offset;
559
560 if (_smooth && (!sharp_faces[poly_i])) {
561 copy_v3_v3(n1, corner_normals[tri[0]]);
562 copy_v3_v3(n2, corner_normals[tri[1]]);
563 copy_v3_v3(n3, corner_normals[tri[2]]);
564
565 mul_mat3_m4_v3(nmat, n1);
566 mul_mat3_m4_v3(nmat, n2);
567 mul_mat3_m4_v3(nmat, n3);
568
569 normalize_v3(n1);
570 normalize_v3(n2);
571 normalize_v3(n3);
572 }
573 else {
574 normal_tri_v3(facenormal, v3, v2, v1);
575
576 copy_v3_v3(n1, facenormal);
577 copy_v3_v3(n2, facenormal);
578 copy_v3_v3(n3, facenormal);
579 }
580
581 uint numTris = countClippedFaces(v1, v2, v3, clip);
582 if (numTris == 0) {
583 continue;
584 }
585
586 bool fm = ffa[poly_i];
587 bool em1 = false, em2 = false, em3 = false;
588
589 if (fed) {
590 em1 = testEdgeMark(mesh, fed, tri, 0);
591 em2 = testEdgeMark(mesh, fed, tri, 1);
592 em3 = testEdgeMark(mesh, fed, tri, 2);
593 }
594
595 if (mat) {
596 tmpMat.setLine(mat->line_col[0], mat->line_col[1], mat->line_col[2], mat->line_col[3]);
597 tmpMat.setDiffuse(mat->r, mat->g, mat->b, 1.0f);
598 tmpMat.setSpecular(mat->specr, mat->specg, mat->specb, 1.0f);
599 tmpMat.setShininess(128.0f);
600 tmpMat.setPriority(mat->line_priority);
601 }
602
603 if (meshMaterials.empty()) {
604 meshMaterials.push_back(mat);
605 meshFrsMaterials.push_back(tmpMat);
606 shape->setFrsMaterial(tmpMat);
607 }
608 else {
609 // find if the Blender material is already in the list
610 uint i = 0;
611 bool found = false;
612
613 for (vector<Material *>::iterator it = meshMaterials.begin(), itend = meshMaterials.end();
614 it != itend;
615 it++, i++)
616 {
617 if (*it == mat) {
618 ls.currentMIndex = i;
619 found = true;
620 break;
621 }
622 }
623
624 if (!found) {
625 meshMaterials.push_back(mat);
626 meshFrsMaterials.push_back(tmpMat);
627 ls.currentMIndex = meshFrsMaterials.size() - 1;
628 }
629 }
630
631 float triCoords[5][3], triNormals[5][3];
632 bool edgeMarks[5]; // edgeMarks[i] is for the edge between i-th and (i+1)-th vertices
633
635 numTris, triCoords, v1, v2, v3, triNormals, n1, n2, n3, edgeMarks, em1, em2, em3, clip);
636 for (i = 0; i < numTris; i++) {
637 addTriangle(&ls,
638 triCoords[0],
639 triCoords[i + 1],
640 triCoords[i + 2],
641 triNormals[0],
642 triNormals[i + 1],
643 triNormals[i + 2],
644 fm,
645 (i == 0) ? edgeMarks[0] : false,
646 edgeMarks[i + 1],
647 (i == numTris - 1) ? edgeMarks[i + 2] : false);
649 }
650 }
651
652 MEM_delete(corner_tris);
653
654 // We might have several times the same vertex. We want a clean
655 // shape with no real-vertex. Here, we are making a cleaning pass.
656 float *cleanVertices = nullptr;
657 uint cvSize;
658 uint *cleanVIndices = nullptr;
659
661 vertices, vSize, VIndices, viSize, &cleanVertices, &cvSize, &cleanVIndices);
662
663 float *cleanNormals = nullptr;
664 uint cnSize;
665 uint *cleanNIndices = nullptr;
666
668 normals, nSize, NIndices, niSize, &cleanNormals, &cnSize, &cleanNIndices);
669
670 // format materials array
671 FrsMaterial **marray = new FrsMaterial *[meshFrsMaterials.size()];
672 uint mindex = 0;
673 for (vector<FrsMaterial>::iterator m = meshFrsMaterials.begin(), mend = meshFrsMaterials.end();
674 m != mend;
675 ++m)
676 {
677 marray[mindex] = new FrsMaterial(*m);
678 ++mindex;
679 }
680
681 // deallocates memory:
682 delete[] vertices;
683 delete[] normals;
684 delete[] VIndices;
685 delete[] NIndices;
686
687 // Fix for degenerated triangles
688 // A degenerate triangle is a triangle such that
689 // 1) A and B are in the same position in the 3D space; or
690 // 2) the distance between point P and line segment AB is zero.
691 // Only those degenerate triangles in the second form are resolved here
692 // by adding a small offset to P, whereas those in the first form are
693 // addressed later in WShape::MakeFace().
694 vector<detri_t> detriList;
695 Vec3r zero(0.0, 0.0, 0.0);
696 uint vi0, vi1, vi2;
697 for (i = 0; i < viSize; i += 3) {
698 detri_t detri;
699 vi0 = cleanVIndices[i];
700 vi1 = cleanVIndices[i + 1];
701 vi2 = cleanVIndices[i + 2];
702 Vec3r v0(cleanVertices[vi0], cleanVertices[vi0 + 1], cleanVertices[vi0 + 2]);
703 Vec3r v1(cleanVertices[vi1], cleanVertices[vi1 + 1], cleanVertices[vi1 + 2]);
704 Vec3r v2(cleanVertices[vi2], cleanVertices[vi2 + 1], cleanVertices[vi2 + 2]);
705 if (v0 == v1 || v0 == v2 || v1 == v2) {
706 continue; // do nothing for now
707 }
708 if (GeomUtils::distPointSegment<Vec3r>(v0, v1, v2) < 1.0e-6) {
709 detri.viP = vi0;
710 detri.viA = vi1;
711 detri.viB = vi2;
712 }
713 else if (GeomUtils::distPointSegment<Vec3r>(v1, v0, v2) < 1.0e-6) {
714 detri.viP = vi1;
715 detri.viA = vi0;
716 detri.viB = vi2;
717 }
718 else if (GeomUtils::distPointSegment<Vec3r>(v2, v0, v1) < 1.0e-6) {
719 detri.viP = vi2;
720 detri.viA = vi0;
721 detri.viB = vi1;
722 }
723 else {
724 continue;
725 }
726
727 detri.v = zero;
728 detri.n = 0;
729 for (uint j = 0; j < viSize; j += 3) {
730 if (i == j) {
731 continue;
732 }
733 vi0 = cleanVIndices[j];
734 vi1 = cleanVIndices[j + 1];
735 vi2 = cleanVIndices[j + 2];
736 Vec3r v0(cleanVertices[vi0], cleanVertices[vi0 + 1], cleanVertices[vi0 + 2]);
737 Vec3r v1(cleanVertices[vi1], cleanVertices[vi1 + 1], cleanVertices[vi1 + 2]);
738 Vec3r v2(cleanVertices[vi2], cleanVertices[vi2 + 1], cleanVertices[vi2 + 2]);
739 if (detri.viP == vi0 && (detri.viA == vi1 || detri.viB == vi1)) {
740 detri.v += (v2 - v0);
741 detri.n++;
742 }
743 else if (detri.viP == vi0 && (detri.viA == vi2 || detri.viB == vi2)) {
744 detri.v += (v1 - v0);
745 detri.n++;
746 }
747 else if (detri.viP == vi1 && (detri.viA == vi0 || detri.viB == vi0)) {
748 detri.v += (v2 - v1);
749 detri.n++;
750 }
751 else if (detri.viP == vi1 && (detri.viA == vi2 || detri.viB == vi2)) {
752 detri.v += (v0 - v1);
753 detri.n++;
754 }
755 else if (detri.viP == vi2 && (detri.viA == vi0 || detri.viB == vi0)) {
756 detri.v += (v1 - v2);
757 detri.n++;
758 }
759 else if (detri.viP == vi2 && (detri.viA == vi1 || detri.viB == vi1)) {
760 detri.v += (v0 - v2);
761 detri.n++;
762 }
763 }
764 if (detri.n > 0) {
765 detri.v.normalizeSafe();
766 }
767 detriList.push_back(detri);
768 }
769
770 if (!detriList.empty()) {
771 vector<detri_t>::iterator v;
772 for (v = detriList.begin(); v != detriList.end(); v++) {
773 detri_t detri = (*v);
774 if (detri.n == 0) {
775 cleanVertices[detri.viP] = cleanVertices[detri.viA];
776 cleanVertices[detri.viP + 1] = cleanVertices[detri.viA + 1];
777 cleanVertices[detri.viP + 2] = cleanVertices[detri.viA + 2];
778 }
779 else if (detri.v.norm() > 0.0) {
780 cleanVertices[detri.viP] += 1.0e-5 * detri.v.x();
781 cleanVertices[detri.viP + 1] += 1.0e-5 * detri.v.y();
782 cleanVertices[detri.viP + 2] += 1.0e-5 * detri.v.z();
783 }
784 }
786 printf("Warning: Object %s contains %lu degenerated triangle%s (strokes may be incorrect)\n",
787 name,
788 ulong(detriList.size()),
789 (detriList.size() > 1) ? "s" : "");
790 }
791 }
792
793 // Create the IndexedFaceSet with the retrieved attributes
794 IndexedFaceSet *rep;
795 rep = new IndexedFaceSet(cleanVertices,
796 cvSize,
797 cleanNormals,
798 cnSize,
799 marray,
800 meshFrsMaterials.size(),
801 nullptr,
802 0,
803 numFaces,
804 numVertexPerFaces,
805 faceStyle,
806 faceEdgeMarks,
807 cleanVIndices,
808 viSize,
809 cleanNIndices,
810 niSize,
811 MIndices,
812 viSize,
813 nullptr,
814 0,
815 0);
816 // sets the id of the rep
817 rep->setId(Id(id, 0));
818 rep->setName(ob->id.name + 2);
819 rep->setLibraryPath(ob->id.lib ? ob->id.lib->filepath : "");
820
821 const BBox<Vec3r> bbox = BBox<Vec3r>(Vec3r(ls.minBBox[0], ls.minBBox[1], ls.minBBox[2]),
822 Vec3r(ls.maxBBox[0], ls.maxBBox[1], ls.maxBBox[2]));
823 rep->setBBox(bbox);
824 shape->AddRep(rep);
825
826 currentMesh->AddChild(shape);
827 _Scene->AddChild(currentMesh);
828}
829
830} /* namespace Freestyle */
CustomData interface, see also DNA_customdata_types.h.
General operations, lookup, etc. for blender objects.
#define NOT_CLIPPED
#define CLIPPED_BY_FAR
#define CLIPPED_BY_NEAR
#define DEG_OBJECT_ITER_BEGIN(settings_, instance_)
#define DEG_OBJECT_ITER_END
#define BLI_assert(a)
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
void insertShapeNode(blender::Object *ob, blender::Mesh *mesh, int id)
void clipLine(float v1[3], float v2[3], float c[3], float z)
void addTriangle(struct LoaderState *ls, float v1[3], float v2[3], float v3[3], float n1[3], float n2[3], float n3[3], bool fm, bool em1, bool em2, bool em3)
int testDegenerateTriangle(float v1[3], float v2[3], float v3[3])
void clipTriangle(int numTris, float triCoords[][3], float v1[3], float v2[3], float v3[3], float triNormals[][3], float n1[3], float n2[3], float n3[3], bool edgeMarks[5], bool em1, bool em2, bool em3, const int clip[3])
blender::Depsgraph * _depsgraph
int countClippedFaces(float v1[3], float v2[3], float v3[3], int clip[3])
BlenderFileLoader(blender::Render *re, blender::ViewLayer *view_layer, blender::Depsgraph *depsgraph)
void setShininess(float s)
void setPriority(int priority)
void setDiffuse(float r, float g, float b, float a)
void setLine(float r, float g, float b, float a)
void setSpecular(float r, float g, float b, float a)
static void CleanIndexedVertexArray(const float *iVertices, uint iVSize, const uint *iIndices, uint iISize, float **oVertices, uint *oVSize, uint **oIndices)
static const FaceEdgeMark EDGE_MARK_V2V3
static const FaceEdgeMark EDGE_MARK_V1V2
static const FaceEdgeMark FACE_MARK
static const FaceEdgeMark EDGE_MARK_V3V1
virtual void AddChild(Node *iChild)
Definition NodeGroup.cpp:16
virtual void AddRep(Rep *iRep)
Definition NodeShape.h:39
void setFrsMaterial(const FrsMaterial &iMaterial)
Definition NodeShape.h:55
void setLibraryPath(const string &path)
Definition Rep.h:152
void setId(const Id &id)
Definition Rep.h:142
virtual void setBBox(const BBox< Vec3f > &iBox)
Definition Rep.h:137
void setName(const string &name)
Definition Rep.h:147
value_type x() const
Definition VecMat.h:489
value_type z() const
Definition VecMat.h:509
value_type y() const
Definition VecMat.h:499
Vec< T, N > & normalizeSafe()
Definition VecMat.h:111
value_type norm() const
Definition VecMat.h:92
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
#define printf
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str)
Definition mallocn.cc:129
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline void copy_v3_v3(ccl_private float *r, const float3 val)
#define G(x, y, z)
real distPointSegment(const T &P, const T &A, const T &B)
Definition GeomUtils.h:32
VecMat::Vec3< real > Vec3r
Definition Geom.h:30
inherits from class Rep
Definition AppCanvas.cpp:20
static bool testEdgeMark(blender::Mesh *mesh, const blender::VArray< bool > &fed, const blender::int3 &tri, int i)
void corner_tris_calc(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, MutableSpan< int3 > corner_tris)
BMesh Iterators.
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
Material * BKE_object_material_get(Object *ob, short act)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
unsigned long ulong
Definition BKE_text.h:57
@ FREESTYLE_FACE_SMOOTHNESS_FLAG
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
ATTR_WARN_UNUSED_RESULT const BMVert * v
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:45
@ G_DEBUG_FREESTYLE
Global G
Definition blender.cc:57
void mul_m4_v3(const float M[4][4], float r[3])
MINLINE bool equals_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
void RE_GetCameraModelMatrix(const Render *re, const Object *camera, float r_modelmat[4][4])
Definition initrender.cc:74
VecBase< int32_t, 2 > int2
VecBase< int32_t, 3 > int3
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET
@ DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY
float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:535
void DEG_iterator_objects_end(BLI_Iterator *iter)
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
MINLINE float normalize_v3(float n[3])
void transpose_m4(float R[4][4])
ATTR_WARN_UNUSED_RESULT const BMVert * v2
VecBase< float, 3 > float3
@ OB_VISIBLE_SELF
Object * RE_GetCamera(Render *re)
Definition initrender.cc:31
const btScalar eps
Definition poly34.cpp:11
IndexedFaceSet::FaceEdgeMark * pm
eFreestyleConfig_Flags flags
char name[258]
Definition DNA_ID.h:433
struct Library * lib
Definition DNA_ID.h:421
char filepath[1024]
Definition DNA_ID.h:562
struct FreestyleConfig freestyle_config