Subversion Repositories gelsvn

Rev

Rev 601 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 601 Rev 622
Line 5... Line 5...
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;
Line 16... Line 17...
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;
23
        return i;
24
            }
-
 
25
		return 0;
24
    return 0;
26
	}
25
  }
27
	
26
	
28
	void TriMesh::compute_normals()
27
  void TriMesh::compute_normals()
29
	{		
28
  {
Line 97... Line 96...
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
}