Subversion Repositories gelsvn

Rev

Rev 601 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 601 Rev 622
Line 1... Line 1...
1
/* ----------------------------------------------------------------------- *
1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
2
* This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
3
* Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
4
* For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
5
* ----------------------------------------------------------------------- */
6
 
6
 
7
#include "../CGLA/Vec3f.h"
7
#include "../CGLA/Vec3f.h"
8
#include <stdio.h>
8
#include <stdio.h>
9
#include <iostream>
9
#include <iostream>
-
 
10
#include <set>
10
 
11
 
11
#include "TriMesh.h"
12
#include "TriMesh.h"
12
 
13
 
13
using namespace std;
14
using namespace std;
14
using namespace CGLA;
15
using namespace CGLA;
15
 
16
 
16
namespace Geometry 
17
namespace Geometry 
17
{
18
{
18
	int TriMesh::find_material(const string& name) const
19
  int TriMesh::find_material(const string& name) const
19
	{
20
  {
20
		for(size_t i=0;i<materials.size(); ++i)
21
    for(size_t i = 0; i < materials.size(); ++i)
21
			{
-
 
22
				if(materials[i].name == name)
22
      if(materials[i].name == name)
23
                    return i;
-
 
24
            }
23
        return i;
25
		return 0;
24
    return 0;
26
	}
25
  }
27
	
26
	
28
	void TriMesh::compute_normals()
27
  void TriMesh::compute_normals()
29
	{		
28
  {
30
		// By default the normal faces are the same as the geometry faces
29
    // By default the normal faces are the same as the geometry faces
31
		// and there are just as many normals as vertices, so we simply
30
    // and there are just as many normals as vertices, so we simply
32
		// copy.
31
    // copy.
33
		normals = geometry;
32
    normals = geometry;
34
 
33
 
35
		const int NV = normals.no_vertices();
34
    const int NV = normals.no_vertices();
36
		// The normals are initialized to zero.
35
    // The normals are initialized to zero.
37
		int i;
36
    int i;
38
		for(i=0;i<NV; ++i)
37
    for(i=0;i<NV; ++i)
39
			normals.vertex_rw(i) = Vec3f(0);
38
      normals.vertex_rw(i) = Vec3f(0);
40
 
39
 
41
		// For each face
40
    // For each face
42
		int NF = geometry.no_faces();
41
    int NF = geometry.no_faces();
43
		for(i=0;i<NF; ++i)
42
    for(i=0;i<NF; ++i)
-
 
43
    {
-
 
44
      // Compute the normal
-
 
45
      const Vec3i& f  = geometry.face(i);
-
 
46
      const Vec3f& p0 = geometry.vertex(f[0]);
-
 
47
      const Vec3f& a  = geometry.vertex(f[1]) - p0;
-
 
48
      const Vec3f& b  = geometry.vertex(f[2]) - p0;
-
 
49
      Vec3f face_normal = cross(a,b);
-
 
50
      float l = sqr_length(face_normal);
-
 
51
      if(l > 0.0f)
-
 
52
        face_normal /= sqrt(l);
-
 
53
 
-
 
54
      // Add the angle weighted normal to each vertex
-
 
55
      for(int j=0;j<3; ++j)
44
      {
56
      {
45
				// Compute the normal
-
 
46
				const Vec3i& f  = geometry.face(i);
-
 
47
				const Vec3f& p0 = geometry.vertex(f[0]);
-
 
48
				const Vec3f& a  = geometry.vertex(f[1]) - p0;
-
 
49
				const Vec3f& b  = geometry.vertex(f[2]) - p0;
-
 
50
				Vec3f face_normal = cross(a,b);
-
 
51
				float l = sqr_length(face_normal);
-
 
52
				if(l > 0.0f)
-
 
53
					face_normal /= sqrt(l);
-
 
54
				
-
 
55
				// Add the angle weighted normal to each vertex
-
 
56
				for(int j=0;j<3; ++j)
-
 
57
					{
-
 
58
						const Vec3f& p0 = geometry.vertex(f[j]);
57
        const Vec3f& p0 = geometry.vertex(f[j]);
59
						Vec3f a = geometry.vertex(f[(j+1)%3]) - p0;
58
        Vec3f a = geometry.vertex(f[(j+1)%3]) - p0;
60
						float l_a = sqr_length(a);
59
        float l_a = sqr_length(a);
61
						if(l_a > 0.0f)
60
        if(l_a > 0.0f)
62
							a /= sqrt(l_a);
61
          a /= sqrt(l_a);
63
						Vec3f b = geometry.vertex(f[(j+2)%3]) - p0;
62
        Vec3f b = geometry.vertex(f[(j+2)%3]) - p0;
64
						float l_b = sqr_length(b);
63
        float l_b = sqr_length(b);
65
						if(l_b > 0.0f)
64
        if(l_b > 0.0f)
66
							b /= sqrt(l_b);
65
          b /= sqrt(l_b);
67
						float d = max(-1.0f, min(1.0f, dot(a,b)));
66
        float d = max(-1.0f, min(1.0f, dot(a,b)));
68
						normals.vertex_rw(f[j]) += face_normal * acos(d);
67
        normals.vertex_rw(f[j]) += face_normal * acos(d);
69
					}
-
 
70
      }
68
      }
-
 
69
    }
71
 
70
 
72
		// Normalize all normals
71
    // Normalize all normals
73
    for(i=0;i<NV; ++i)
72
    for(i=0;i<NV; ++i)
74
			{
73
    {
75
				float l_vert_rw = sqr_length(normals.vertex_rw(i));
74
      float l_vert_rw = sqr_length(normals.vertex_rw(i));
76
				if(l_vert_rw > 0.0f)
75
      if(l_vert_rw > 0.0f)
77
					normals.vertex_rw(i) /= sqrt(l_vert_rw);
76
        normals.vertex_rw(i) /= sqrt(l_vert_rw);
78
			}
77
    }
79
	}
78
  }
80
 
79
 
81
	void TriMesh::compute_areas()
80
  void TriMesh::compute_areas()
82
	{
81
  {
83
    int no_of_faces = geometry.no_faces();
82
    int no_of_faces = geometry.no_faces();
84
    surface_area = 0.0f;
83
    surface_area = 0.0f;
85
    face_areas.resize(no_of_faces);
84
    face_areas.resize(no_of_faces);
86
    face_area_cdf.resize(no_of_faces);
85
    face_area_cdf.resize(no_of_faces);
87
		for(int i = 0; i < no_of_faces; ++i)
86
    for(int i = 0; i < no_of_faces; ++i)
88
    {
87
    {
89
			const Vec3i& f  = geometry.face(i);
88
      const Vec3i& f  = geometry.face(i);
90
			const Vec3f& p0 = geometry.vertex(f[0]);
89
      const Vec3f& p0 = geometry.vertex(f[0]);
91
			const Vec3f& a  = geometry.vertex(f[1]) - p0;
90
      const Vec3f& a  = geometry.vertex(f[1]) - p0;
92
			const Vec3f& b  = geometry.vertex(f[2]) - p0;
91
      const Vec3f& b  = geometry.vertex(f[2]) - p0;
93
      face_areas[i] = 0.5f*cross(a, b).length();
92
      face_areas[i] = 0.5f*cross(a, b).length();
94
      face_area_cdf[i] = surface_area + face_areas[i];
93
      face_area_cdf[i] = surface_area + face_areas[i];
95
      surface_area += face_areas[i];
94
      surface_area += face_areas[i];
96
    }
95
    }
97
    if(surface_area > 0.0f)
96
    if(surface_area > 0.0f)
98
      for(int i = 0; i < no_of_faces; ++i)
97
      for(int i = 0; i < no_of_faces; ++i)
99
        face_area_cdf[i] /= surface_area;
98
        face_area_cdf[i] /= surface_area;
100
	}
99
  }
101
 
100
 
102
  bool TriMesh::get_bbox(CGLA::Vec3f& p0, CGLA::Vec3f& p7) const
101
  bool TriMesh::get_bbox(Vec3f& p0, Vec3f& p7) const
103
  {
102
  {
104
    if(geometry.no_vertices() == 0)
103
    if(geometry.no_vertices() == 0)
105
      return false;
104
      return false;
106
 
105
 
107
    int i;
106
    int i;
108
    p0 = geometry.vertex(0);
107
    p0 = geometry.vertex(0);
109
    p7 = geometry.vertex(0);
108
    p7 = geometry.vertex(0);
110
    for(i=1;i<geometry.no_vertices();i++) 
109
    for(i = 1; i < geometry.no_vertices(); ++i) 
111
      {
110
    {
112
				p0 = v_min(geometry.vertex(i), p0);
111
      p0 = v_min(geometry.vertex(i), p0);
113
				p7 = v_max(geometry.vertex(i), p7);
112
      p7 = v_max(geometry.vertex(i), p7);
114
      }
113
    }
115
    return true;
114
    return true;
116
  }
115
  }
117
 
116
 
118
  bool TriMesh::get_bsphere(CGLA::Vec3f& c, float& r) const
117
  bool TriMesh::get_bsphere(Vec3f& c, float& r) const
119
  {
118
  {
120
    Vec3f p0,p7;
119
    Vec3f p0,p7;
121
    if(!get_bbox(p0, p7))
120
    if(!get_bbox(p0, p7))
122
      return false;
121
      return false;
123
 
122
 
Line 125... Line 124...
125
    c = p0 + rad;
124
    c = p0 + rad;
126
    r = rad.length();
125
    r = rad.length();
127
    return true;
126
    return true;
128
  }
127
  }
129
 
128
 
130
  void TriMesh::transform(CGLA::Mat4x4f m)
129
  void TriMesh::transform(const Mat4x4f& m)
131
  {
130
  {
132
    for(int i = 0; i < geometry.no_vertices(); ++i)
131
    for(int i = 0; i < geometry.no_vertices(); ++i)
133
      geometry.vertex_rw(i) = m.mul_3D_point(geometry.vertex(i));
132
      geometry.vertex_rw(i) = m.mul_3D_point(geometry.vertex(i));
134
    for(int i = 0; i < normals.no_vertices(); ++i)
133
    for(int i = 0; i < normals.no_vertices(); ++i)
135
      normals.vertex_rw(i) = normalize(m.mul_3D_vector(normals.vertex(i)));
134
      normals.vertex_rw(i) = normalize(m.mul_3D_vector(normals.vertex(i)));
136
  }
135
  }
-
 
136
 
-
 
137
  void TriMesh::tex_transform(const Mat4x4f& m)
-
 
138
  {
-
 
139
    for(int i = 0; i < texcoords.no_vertices(); ++i)
-
 
140
      texcoords.vertex_rw(i) = m.mul_3D_point(texcoords.vertex(i));
-
 
141
  }
-
 
142
 
-
 
143
  void TriMesh::tex_transform(const Mat4x4f& m, const string& material)
-
 
144
  {
-
 
145
    set<int> v_touched;
-
 
146
    int m_idx = find_material(material);
-
 
147
    for(int i = 0; i < texcoords.no_faces(); ++i)
-
 
148
      if(mat_idx[i] == m_idx)
-
 
149
      {
-
 
150
        Vec3i face = texcoords.face(i);
-
 
151
        for(int j = 0; j < 3; ++j)
-
 
152
        {
-
 
153
          int v_idx = face[j];
-
 
154
          if(v_touched.count(v_idx) == 0)
-
 
155
          {
-
 
156
            v_touched.insert(v_idx);
-
 
157
            texcoords.vertex_rw(v_idx) = m.mul_3D_point(texcoords.vertex(v_idx));
-
 
158
          }
-
 
159
        }
-
 
160
      }
-
 
161
  }
137
}
162
}