38 _minEdgeSize = DBL_MAX;
52 cout <<
"\n=== Importing triangular meshes into Blender ===" << endl;
58 if (
_re->clip_start < 0.0f) {
114#define CLIPPED_BY_NEAR -1
116#define CLIPPED_BY_FAR 1
123 int numClipped,
sum, numTris = 0;
128 numClipped =
sum = 0;
129 for (
int i = 0;
i < 3;
i++) {
152 switch (numClipped) {
196 for (
int i = 0;
i < 3;
i++) {
199 double t = (
z - p[2]) / d[2];
200 c[0] = p[0] + t * d[0];
201 c[1] = p[1] + t * d[1];
209 float triCoords[][3],
213 float triNormals[][3],
237 for (
i = 0;
i < 3;
i++) {
242 edgeMarks[k] = em[
i];
247 edgeMarks[k] =
false;
251 else if (clip[
i] != clip[j]) {
255 edgeMarks[k] = em[
i];
261 edgeMarks[k] = em[
i];
265 edgeMarks[k] =
false;
286 float *fv[3], *fn[3];
305 for (
i = 0;
i < 3;
i++) {
311 for (j = 0; j < 3; j++) {
323 if (_minEdgeSize >
len) {
362 const float eps = 1.0e-6;
363 const float eps_sq =
eps *
eps;
367 bool verbose = (area < 1.0e-6);
373 printf(
"BlenderFileLoader::testDegenerateTriangle = 1\n");
384 printf(
"BlenderFileLoader::testDegenerateTriangle = 2\n");
391 printf(
"BlenderFileLoader::testDegenerateTriangle = 0\n");
400 const Span<int> corner_verts = mesh->corner_verts();
401 const Span<int> corner_edges = mesh->corner_edges();
403 const int corner = tri[
i];
404 const int corner_next = tri[(
i + 1) % 3];
407 if (!
ELEM(corner_verts[corner_next], edge[0], edge[1])) {
418 char *name = ob->
id.
name + 2;
420 const Span<float3> vert_positions = mesh->vert_positions();
422 const Span<int> corner_verts = mesh->corner_verts();
428 vert_positions, mesh_polys, corner_verts, {corner_tris, tottri});
440 float viewinv[4][4], viewmat[4][4];
445 float obmat[4][4], nmat[4][4];
446 mul_m4_m4m4(obmat, viewmat, ob->object_to_world().ptr());
453 float v1[3],
v2[3], v3[3];
454 float n1[3], n2[3], n3[3], facenormal[3];
456 for (
int a = 0;
a < tottri;
a++) {
457 const int3 &tri = corner_tris[
a];
459 copy_v3_v3(v1, vert_positions[corner_verts[tri[0]]]);
461 copy_v3_v3(v3, vert_positions[corner_verts[tri[2]]]);
475 cout <<
"numFaces " << numFaces << endl;
487 uint vSize = 3 * 3 * numFaces;
488 float *vertices =
new float[vSize];
490 float *
normals =
new float[nSize];
491 uint *numVertexPerFaces =
new uint[numFaces];
492 vector<Material *> meshMaterials;
493 vector<FrsMaterial> meshFrsMaterials;
497 for (
i = 0;
i < numFaces;
i++) {
499 numVertexPerFaces[
i] = 3;
504 uint viSize = 3 * numFaces;
506 uint niSize = viSize;
513 ls.
pm = faceEdgeMarks;
530 for (
int a = 0;
a < tottri;
a++) {
531 const int3 &tri = corner_tris[
a];
532 const int poly_i = tri_faces[
a];
535 copy_v3_v3(v1, vert_positions[corner_verts[tri[0]]]);
537 copy_v3_v3(v3, vert_positions[corner_verts[tri[2]]]);
547 if (
_smooth && (!sharp_faces[poly_i])) {
574 bool em1 =
false, em2 =
false, em3 =
false;
590 if (meshMaterials.empty()) {
591 meshMaterials.push_back(mat);
592 meshFrsMaterials.push_back(tmpMat);
600 for (vector<Material *>::iterator it = meshMaterials.begin(), itend = meshMaterials.end();
612 meshMaterials.push_back(mat);
613 meshFrsMaterials.push_back(tmpMat);
618 float triCoords[5][3], triNormals[5][3];
622 numTris, triCoords, v1,
v2, v3, triNormals, n1, n2, n3, edgeMarks, em1, em2, em3, clip);
623 for (
i = 0;
i < numTris;
i++) {
632 (
i == 0) ? edgeMarks[0] :
false,
634 (
i == numTris - 1) ? edgeMarks[
i + 2] :
false);
643 float *cleanVertices =
nullptr;
645 uint *cleanVIndices =
nullptr;
648 vertices, vSize, VIndices, viSize, &cleanVertices, &cvSize, &cleanVIndices);
650 float *cleanNormals =
nullptr;
652 uint *cleanNIndices =
nullptr;
655 normals, nSize, NIndices, niSize, &cleanNormals, &cnSize, &cleanNIndices);
660 for (vector<FrsMaterial>::iterator m = meshFrsMaterials.begin(), mend = meshFrsMaterials.end();
681 vector<detri_t> detriList;
682 Vec3r zero(0.0, 0.0, 0.0);
684 for (
i = 0;
i < viSize;
i += 3) {
686 vi0 = cleanVIndices[
i];
687 vi1 = cleanVIndices[
i + 1];
688 vi2 = cleanVIndices[
i + 2];
689 Vec3r v0(cleanVertices[vi0], cleanVertices[vi0 + 1], cleanVertices[vi0 + 2]);
690 Vec3r v1(cleanVertices[vi1], cleanVertices[vi1 + 1], cleanVertices[vi1 + 2]);
691 Vec3r v2(cleanVertices[vi2], cleanVertices[vi2 + 1], cleanVertices[vi2 + 2]);
692 if (v0 == v1 || v0 ==
v2 || v1 ==
v2) {
716 for (
uint j = 0; j < viSize; j += 3) {
720 vi0 = cleanVIndices[j];
721 vi1 = cleanVIndices[j + 1];
722 vi2 = cleanVIndices[j + 2];
723 Vec3r v0(cleanVertices[vi0], cleanVertices[vi0 + 1], cleanVertices[vi0 + 2]);
724 Vec3r v1(cleanVertices[vi1], cleanVertices[vi1 + 1], cleanVertices[vi1 + 2]);
725 Vec3r v2(cleanVertices[vi2], cleanVertices[vi2 + 1], cleanVertices[vi2 + 2]);
726 if (detri.
viP == vi0 && (detri.
viA == vi1 || detri.
viB == vi1)) {
727 detri.
v += (
v2 - v0);
730 else if (detri.
viP == vi0 && (detri.
viA == vi2 || detri.
viB == vi2)) {
731 detri.
v += (v1 - v0);
734 else if (detri.
viP == vi1 && (detri.
viA == vi0 || detri.
viB == vi0)) {
735 detri.
v += (
v2 - v1);
738 else if (detri.
viP == vi1 && (detri.
viA == vi2 || detri.
viB == vi2)) {
739 detri.
v += (v0 - v1);
742 else if (detri.
viP == vi2 && (detri.
viA == vi0 || detri.
viB == vi0)) {
743 detri.
v += (v1 -
v2);
746 else if (detri.
viP == vi2 && (detri.
viA == vi1 || detri.
viB == vi1)) {
747 detri.
v += (v0 -
v2);
754 detriList.push_back(detri);
757 if (!detriList.empty()) {
758 vector<detri_t>::iterator
v;
759 for (
v = detriList.begin();
v != detriList.end();
v++) {
762 cleanVertices[detri.
viP] = cleanVertices[detri.
viA];
763 cleanVertices[detri.
viP + 1] = cleanVertices[detri.
viA + 1];
764 cleanVertices[detri.
viP + 2] = cleanVertices[detri.
viA + 2];
766 else if (detri.
v.
norm() > 0.0) {
767 cleanVertices[detri.
viP] += 1.0e-5 * detri.
v.
x();
768 cleanVertices[detri.
viP + 1] += 1.0e-5 * detri.
v.
y();
769 cleanVertices[detri.
viP + 2] += 1.0e-5 * detri.
v.
z();
773 printf(
"Warning: Object %s contains %lu degenerated triangle%s (strokes may be incorrect)\n",
775 ulong(detriList.size()),
776 (detriList.size() > 1) ?
"s" :
"");
787 meshFrsMaterials.size(),
814 _Scene->AddChild(currentMesh);
CustomData interface, see also DNA_customdata_types.h.
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
Material * BKE_object_material_get(Object *ob, short act)
General operations, lookup, etc. for blender objects.
void BKE_object_to_mesh_clear(Object *object)
Mesh * BKE_object_to_mesh(Depsgraph *depsgraph, Object *object, bool preserve_all_data_layers)
int BKE_object_visibility(const Object *ob, int dag_eval_mode)
float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3])
float area_tri_v3(const float v1[3], const float v2[3], const float v3[3])
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void mul_m4_v3(const float M[4][4], float r[3])
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void transpose_m4(float R[4][4])
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE bool equals_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE float normalize_v3(float n[3])
#define DEG_OBJECT_ITER_BEGIN(settings_, instance_)
#define DEG_OBJECT_ITER_END
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY
@ DEG_ITER_OBJECT_FLAG_VISIBLE
@ DEG_ITER_OBJECT_FLAG_DUPLI
@ DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET
@ FREESTYLE_FACE_SMOOTHNESS_FLAG
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert * v
BPy_StructRNA * depsgraph
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
static T sum(const btAlignedObjectArray< T > &items)
BlenderFileLoader(Render *re, ViewLayer *view_layer, Depsgraph *depsgraph)
RenderMonitor * _pRenderMonitor
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 insertShapeNode(Object *ob, Mesh *mesh, int id)
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])
virtual ~BlenderFileLoader()
int countClippedFaces(float v1[3], float v2[3], float v3[3], int clip[3])
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)
virtual void AddRep(Rep *iRep)
void setFrsMaterial(const FrsMaterial &iMaterial)
void setLibraryPath(const string &path)
virtual void setBBox(const BBox< Vec3f > &iBox)
void setName(const string &name)
Vec< T, N > & normalizeSafe()
GAttributeReader lookup_or_default(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type, const void *default_value=nullptr) const
static float normals[][3]
void RE_GetCameraModelMatrix(const Render *re, const Object *camera, float r_modelmat[4][4])
Object * RE_GetCamera(Render *re)
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
void MEM_freeN(void *vmemh)
real distPointSegment(const T &P, const T &A, const T &B)
VecMat::Vec3< real > Vec3r
static bool testEdgeMark(Mesh *mesh, const FreestyleEdge *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)
VecBase< int32_t, 2 > int2
VecBase< int32_t, 3 > int3
VecBase< float, 3 > float3
IndexedFaceSet::FaceEdgeMark * pm
struct FreestyleConfig freestyle_config