Subversion Repositories gelsvn

Rev

Rev 595 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 595 Rev 601
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
 
10
 
11
#include "TriMesh.h"
11
#include "TriMesh.h"
12
 
12
 
13
using namespace std;
13
using namespace std;
14
using namespace CGLA;
14
using namespace CGLA;
15
 
15
 
16
namespace Geometry 
16
namespace Geometry 
17
{
17
{
18
	int TriMesh::find_material(const string& name) const
18
	int TriMesh::find_material(const string& name) const
19
	{
19
	{
20
		for(size_t i=0;i<materials.size(); ++i)
20
		for(size_t i=0;i<materials.size(); ++i)
21
			{
21
			{
22
				if(materials[i].name == name)
22
				if(materials[i].name == name)
23
                    return i;
23
                    return i;
24
            }
24
            }
25
		return 0;
25
		return 0;
26
	}
26
	}
27
	
27
	
28
	void TriMesh::compute_normals()
28
	void TriMesh::compute_normals()
29
	{		
29
	{		
30
		// By default the normal faces are the same as the geometry faces
30
		// 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
31
		// and there are just as many normals as vertices, so we simply
32
		// copy.
32
		// copy.
33
		normals = geometry;
33
		normals = geometry;
34
 
34
 
35
		const int NV = normals.no_vertices();
35
		const int NV = normals.no_vertices();
36
		// The normals are initialized to zero.
36
		// The normals are initialized to zero.
37
		int i;
37
		int i;
38
		for(i=0;i<NV; ++i)
38
		for(i=0;i<NV; ++i)
39
			normals.vertex_rw(i) = Vec3f(0);
39
			normals.vertex_rw(i) = Vec3f(0);
40
 
40
 
41
		// For each face
41
		// For each face
42
		int NF = geometry.no_faces();
42
		int NF = geometry.no_faces();
43
		for(i=0;i<NF; ++i)
43
		for(i=0;i<NF; ++i)
44
      {
44
      {
45
				// Compute the normal
45
				// Compute the normal
46
				const Vec3i& f  = geometry.face(i);
46
				const Vec3i& f  = geometry.face(i);
47
				const Vec3f& p0 = geometry.vertex(f[0]);
47
				const Vec3f& p0 = geometry.vertex(f[0]);
48
				const Vec3f& a  = geometry.vertex(f[1]) - p0;
48
				const Vec3f& a  = geometry.vertex(f[1]) - p0;
49
				const Vec3f& b  = geometry.vertex(f[2]) - p0;
49
				const Vec3f& b  = geometry.vertex(f[2]) - p0;
50
				Vec3f face_normal = cross(a,b);
50
				Vec3f face_normal = cross(a,b);
51
				float l = sqr_length(face_normal);
51
				float l = sqr_length(face_normal);
52
				if(l > 0.0f)
52
				if(l > 0.0f)
53
					face_normal /= sqrt(l);
53
					face_normal /= sqrt(l);
54
				
54
				
55
				// Add the angle weighted normal to each vertex
55
				// Add the angle weighted normal to each vertex
56
				for(int j=0;j<3; ++j)
56
				for(int j=0;j<3; ++j)
57
					{
57
					{
58
						const Vec3f& p0 = geometry.vertex(f[j]);
58
						const Vec3f& p0 = geometry.vertex(f[j]);
59
						Vec3f a = geometry.vertex(f[(j+1)%3]) - p0;
59
						Vec3f a = geometry.vertex(f[(j+1)%3]) - p0;
60
						float l_a = sqr_length(a);
60
						float l_a = sqr_length(a);
61
						if(l_a > 0.0f)
61
						if(l_a > 0.0f)
62
							a /= sqrt(l_a);
62
							a /= sqrt(l_a);
63
						Vec3f b = geometry.vertex(f[(j+2)%3]) - p0;
63
						Vec3f b = geometry.vertex(f[(j+2)%3]) - p0;
64
						float l_b = sqr_length(b);
64
						float l_b = sqr_length(b);
65
						if(l_b > 0.0f)
65
						if(l_b > 0.0f)
66
							b /= sqrt(l_b);
66
							b /= sqrt(l_b);
67
						float d = max(-1.0f, min(1.0f, dot(a,b)));
67
						float d = max(-1.0f, min(1.0f, dot(a,b)));
68
						normals.vertex_rw(f[j]) += face_normal * acos(d);
68
						normals.vertex_rw(f[j]) += face_normal * acos(d);
69
					}
69
					}
70
      }
70
      }
71
 
71
 
72
		// Normalize all normals
72
		// Normalize all normals
73
    for(i=0;i<NV; ++i)
73
    for(i=0;i<NV; ++i)
74
			{
74
			{
75
				float l_vert_rw = sqr_length(normals.vertex_rw(i));
75
				float l_vert_rw = sqr_length(normals.vertex_rw(i));
76
				if(l_vert_rw > 0.0f)
76
				if(l_vert_rw > 0.0f)
77
					normals.vertex_rw(i) /= sqrt(l_vert_rw);
77
					normals.vertex_rw(i) /= sqrt(l_vert_rw);
78
			}
78
			}
79
	}
79
	}
80
 
80
 
81
	void TriMesh::compute_areas()
81
	void TriMesh::compute_areas()
82
	{
82
	{
83
    int no_of_faces = geometry.no_faces();
83
    int no_of_faces = geometry.no_faces();
84
    surface_area = 0.0f;
84
    surface_area = 0.0f;
85
    face_areas.resize(no_of_faces);
85
    face_areas.resize(no_of_faces);
86
    face_area_cdf.resize(no_of_faces);
86
    face_area_cdf.resize(no_of_faces);
87
		for(int i = 0; i < no_of_faces; ++i)
87
		for(int i = 0; i < no_of_faces; ++i)
88
    {
88
    {
89
			const Vec3i& f  = geometry.face(i);
89
			const Vec3i& f  = geometry.face(i);
90
			const Vec3f& p0 = geometry.vertex(f[0]);
90
			const Vec3f& p0 = geometry.vertex(f[0]);
91
			const Vec3f& a  = geometry.vertex(f[1]) - p0;
91
			const Vec3f& a  = geometry.vertex(f[1]) - p0;
92
			const Vec3f& b  = geometry.vertex(f[2]) - p0;
92
			const Vec3f& b  = geometry.vertex(f[2]) - p0;
93
      face_areas[i] = 0.5f*cross(a, b).length();
93
      face_areas[i] = 0.5f*cross(a, b).length();
94
      face_area_cdf[i] = surface_area + face_areas[i];
94
      face_area_cdf[i] = surface_area + face_areas[i];
95
      surface_area += face_areas[i];
95
      surface_area += face_areas[i];
96
    }
96
    }
97
    if(surface_area > 0.0f)
97
    if(surface_area > 0.0f)
98
      for(int i = 0; i < no_of_faces; ++i)
98
      for(int i = 0; i < no_of_faces; ++i)
99
        face_area_cdf[i] /= surface_area;
99
        face_area_cdf[i] /= surface_area;
100
	}
100
	}
101
 
101
 
102
  bool TriMesh::get_bbox(CGLA::Vec3f& p0, CGLA::Vec3f& p7) const
102
  bool TriMesh::get_bbox(CGLA::Vec3f& p0, CGLA::Vec3f& p7) const
103
  {
103
  {
104
    if(geometry.no_vertices() == 0)
104
    if(geometry.no_vertices() == 0)
105
      return false;
105
      return false;
106
 
106
 
107
    int i;
107
    int i;
108
    p0 = geometry.vertex(0);
108
    p0 = geometry.vertex(0);
109
    p7 = geometry.vertex(0);
109
    p7 = geometry.vertex(0);
110
    for(i=1;i<geometry.no_vertices();i++) 
110
    for(i=1;i<geometry.no_vertices();i++) 
111
      {
111
      {
112
				p0 = v_min(geometry.vertex(i), p0);
112
				p0 = v_min(geometry.vertex(i), p0);
113
				p7 = v_max(geometry.vertex(i), p7);
113
				p7 = v_max(geometry.vertex(i), p7);
114
      }
114
      }
115
    return true;
115
    return true;
116
  }
116
  }
117
 
117
 
118
  bool TriMesh::get_bsphere(CGLA::Vec3f& c, float& r) const
118
  bool TriMesh::get_bsphere(CGLA::Vec3f& c, float& r) const
119
  {
119
  {
120
    Vec3f p0,p7;
120
    Vec3f p0,p7;
121
    if(!get_bbox(p0, p7))
121
    if(!get_bbox(p0, p7))
122
      return false;
122
      return false;
123
 
123
 
124
    Vec3f rad = (p7 - p0)/2.0;
124
    Vec3f rad = (p7 - p0)/2.0;
125
    c = p0 + rad;
125
    c = p0 + rad;
126
    r = rad.length();
126
    r = rad.length();
127
    return true;
127
    return true;
128
  }
128
  }
129
 
129
 
130
  void TriMesh::transform(CGLA::Mat4x4f m)
130
  void TriMesh::transform(CGLA::Mat4x4f m)
131
  {
131
  {
132
    for(int i = 0; i < geometry.no_vertices(); ++i)
132
    for(int i = 0; i < geometry.no_vertices(); ++i)
133
      geometry.vertex_rw(i) = m.mul_3D_point(geometry.vertex(i));
133
      geometry.vertex_rw(i) = m.mul_3D_point(geometry.vertex(i));
134
    for(int i = 0; i < normals.no_vertices(); ++i)
134
    for(int i = 0; i < normals.no_vertices(); ++i)
135
      normals.vertex_rw(i) = normalize(m.mul_3D_vector(normals.vertex(i)));
135
      normals.vertex_rw(i) = normalize(m.mul_3D_vector(normals.vertex(i)));
136
  }
136
  }
137
}
137
}
138
 
138