tavli

annotate src/geom.cc @ 6:a0d30f6f20d4

- texture coordinate generation in class Mesh - wood texture
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 26 Jun 2015 04:22:06 +0300
parents
children
rev   line source
nuclear@0 1 #include <algorithm>
nuclear@0 2 #include <float.h>
nuclear@0 3 #include "geom.h"
nuclear@0 4
nuclear@0 5 GeomObject::~GeomObject()
nuclear@0 6 {
nuclear@0 7 }
nuclear@0 8
nuclear@0 9
nuclear@0 10 Sphere::Sphere()
nuclear@0 11 {
nuclear@0 12 radius = 1.0;
nuclear@0 13 }
nuclear@0 14
nuclear@0 15 Sphere::Sphere(const Vector3 &cent, float radius)
nuclear@0 16 : center(cent)
nuclear@0 17 {
nuclear@0 18 this->radius = radius;
nuclear@0 19 }
nuclear@0 20
nuclear@0 21 void Sphere::set_union(const GeomObject *obj1, const GeomObject *obj2)
nuclear@0 22 {
nuclear@0 23 const Sphere *sph1 = dynamic_cast<const Sphere*>(obj1);
nuclear@0 24 const Sphere *sph2 = dynamic_cast<const Sphere*>(obj2);
nuclear@0 25
nuclear@0 26 if(!sph1 || !sph2) {
nuclear@0 27 fprintf(stderr, "Sphere::set_union: arguments must be spheres");
nuclear@0 28 return;
nuclear@0 29 }
nuclear@0 30
nuclear@0 31 float dist = (sph1->center - sph2->center).length();
nuclear@0 32 float surf_dist = dist - (sph1->radius + sph2->radius);
nuclear@0 33 float d1 = sph1->radius + surf_dist / 2.0;
nuclear@0 34 float d2 = sph2->radius + surf_dist / 2.0;
nuclear@0 35 float t = d1 / (d1 + d2);
nuclear@0 36
nuclear@0 37 if(t < 0.0) t = 0.0;
nuclear@0 38 if(t > 1.0) t = 1.0;
nuclear@0 39
nuclear@0 40 center = sph1->center * t + sph2->center * (1.0 - t);
nuclear@0 41 radius = std::max(dist * t + sph2->radius, dist * (1.0f - t) + sph1->radius);
nuclear@0 42 }
nuclear@0 43
nuclear@0 44 void Sphere::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
nuclear@0 45 {
nuclear@0 46 fprintf(stderr, "Sphere::intersection undefined\n");
nuclear@0 47 }
nuclear@0 48
nuclear@0 49 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
nuclear@0 50 {
nuclear@0 51 float a = dot_product(ray.dir, ray.dir);
nuclear@0 52 float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
nuclear@0 53 2.0 * ray.dir.y * (ray.origin.y - center.y) +
nuclear@0 54 2.0 * ray.dir.z * (ray.origin.z - center.z);
nuclear@0 55 float c = dot_product(ray.origin, ray.origin) + dot_product(center, center) -
nuclear@0 56 2.0 * dot_product(ray.origin, center) - radius * radius;
nuclear@0 57
nuclear@0 58 float discr = b * b - 4.0 * a * c;
nuclear@0 59 if(discr < 1e-4) {
nuclear@0 60 return false;
nuclear@0 61 }
nuclear@0 62
nuclear@0 63 float sqrt_discr = sqrt(discr);
nuclear@0 64 float t0 = (-b + sqrt_discr) / (2.0 * a);
nuclear@0 65 float t1 = (-b - sqrt_discr) / (2.0 * a);
nuclear@0 66
nuclear@0 67 if(t0 < 1e-4)
nuclear@0 68 t0 = t1;
nuclear@0 69 if(t1 < 1e-4)
nuclear@0 70 t1 = t0;
nuclear@0 71
nuclear@0 72 float t = t0 < t1 ? t0 : t1;
nuclear@0 73 if(t < 1e-4) {
nuclear@0 74 return false;
nuclear@0 75 }
nuclear@0 76
nuclear@0 77 // fill the HitPoint structure
nuclear@0 78 if(hit) {
nuclear@0 79 hit->obj = this;
nuclear@0 80 hit->dist = t;
nuclear@0 81 hit->pos = ray.origin + ray.dir * t;
nuclear@0 82 hit->normal = (hit->pos - center) / radius;
nuclear@0 83 }
nuclear@0 84 return true;
nuclear@0 85 }
nuclear@0 86
nuclear@0 87
nuclear@0 88 AABox::AABox()
nuclear@0 89 {
nuclear@0 90 }
nuclear@0 91
nuclear@0 92 AABox::AABox(const Vector3 &vmin, const Vector3 &vmax)
nuclear@0 93 : min(vmin), max(vmax)
nuclear@0 94 {
nuclear@0 95 }
nuclear@0 96
nuclear@0 97 void AABox::set_union(const GeomObject *obj1, const GeomObject *obj2)
nuclear@0 98 {
nuclear@0 99 const AABox *box1 = dynamic_cast<const AABox*>(obj1);
nuclear@0 100 const AABox *box2 = dynamic_cast<const AABox*>(obj2);
nuclear@0 101
nuclear@0 102 if(!box1 || !box2) {
nuclear@0 103 fprintf(stderr, "AABox::set_union: arguments must be AABoxes too\n");
nuclear@0 104 return;
nuclear@0 105 }
nuclear@0 106
nuclear@0 107 min.x = std::min(box1->min.x, box2->min.x);
nuclear@0 108 min.y = std::min(box1->min.y, box2->min.y);
nuclear@0 109 min.z = std::min(box1->min.z, box2->min.z);
nuclear@0 110
nuclear@0 111 max.x = std::max(box1->max.x, box2->max.x);
nuclear@0 112 max.y = std::max(box1->max.y, box2->max.y);
nuclear@0 113 max.z = std::max(box1->max.z, box2->max.z);
nuclear@0 114 }
nuclear@0 115
nuclear@0 116 void AABox::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
nuclear@0 117 {
nuclear@0 118 const AABox *box1 = dynamic_cast<const AABox*>(obj1);
nuclear@0 119 const AABox *box2 = dynamic_cast<const AABox*>(obj2);
nuclear@0 120
nuclear@0 121 if(!box1 || !box2) {
nuclear@0 122 fprintf(stderr, "AABox::set_intersection: arguments must be AABoxes too\n");
nuclear@0 123 return;
nuclear@0 124 }
nuclear@0 125
nuclear@0 126 for(int i=0; i<3; i++) {
nuclear@0 127 min[i] = std::max(box1->min[i], box2->min[i]);
nuclear@0 128 max[i] = std::min(box1->max[i], box2->max[i]);
nuclear@0 129
nuclear@0 130 if(max[i] < min[i]) {
nuclear@0 131 max[i] = min[i];
nuclear@0 132 }
nuclear@0 133 }
nuclear@0 134 }
nuclear@0 135
nuclear@0 136 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
nuclear@0 137 {
nuclear@0 138 Vector3 param[2] = {min, max};
nuclear@0 139 Vector3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
nuclear@0 140 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
nuclear@0 141
nuclear@0 142 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
nuclear@0 143 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
nuclear@0 144 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
nuclear@0 145 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
nuclear@0 146
nuclear@0 147 if(tmin > tymax || tymin > tmax) {
nuclear@0 148 return false;
nuclear@0 149 }
nuclear@0 150 if(tymin > tmin) {
nuclear@0 151 tmin = tymin;
nuclear@0 152 }
nuclear@0 153 if(tymax < tmax) {
nuclear@0 154 tmax = tymax;
nuclear@0 155 }
nuclear@0 156
nuclear@0 157 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
nuclear@0 158 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
nuclear@0 159
nuclear@0 160 if(tmin > tzmax || tzmin > tmax) {
nuclear@0 161 return false;
nuclear@0 162 }
nuclear@0 163 if(tzmin > tmin) {
nuclear@0 164 tmin = tzmin;
nuclear@0 165 }
nuclear@0 166 if(tzmax < tmax) {
nuclear@0 167 tmax = tzmax;
nuclear@0 168 }
nuclear@0 169
nuclear@0 170 float t = tmin < 1e-4 ? tmax : tmin;
nuclear@0 171 if(t >= 1e-4) {
nuclear@0 172
nuclear@0 173 if(hit) {
nuclear@0 174 hit->obj = this;
nuclear@0 175 hit->dist = t;
nuclear@0 176 hit->pos = ray.origin + ray.dir * t;
nuclear@0 177
nuclear@0 178 float min_dist = FLT_MAX;
nuclear@0 179 Vector3 offs = min + (max - min) / 2.0;
nuclear@0 180 Vector3 local_hit = hit->pos - offs;
nuclear@0 181
nuclear@0 182 static const Vector3 axis[] = {
nuclear@0 183 Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)
nuclear@0 184 };
nuclear@0 185 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
nuclear@0 186
nuclear@0 187 for(int i=0; i<3; i++) {
nuclear@0 188 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
nuclear@0 189 if(dist < min_dist) {
nuclear@0 190 min_dist = dist;
nuclear@0 191 hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
nuclear@0 192 //hit->texcoord = Vector2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
nuclear@0 193 }
nuclear@0 194 }
nuclear@0 195 }
nuclear@0 196 return true;
nuclear@0 197 }
nuclear@0 198 return false;
nuclear@0 199
nuclear@0 200 }
nuclear@0 201
nuclear@0 202 Plane::Plane()
nuclear@0 203 : normal(0.0, 1.0, 0.0)
nuclear@0 204 {
nuclear@0 205 }
nuclear@0 206
nuclear@0 207 Plane::Plane(const Vector3 &p, const Vector3 &norm)
nuclear@0 208 : pt(p)
nuclear@0 209 {
nuclear@0 210 normal = norm.normalized();
nuclear@0 211 }
nuclear@0 212
nuclear@0 213 Plane::Plane(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3)
nuclear@0 214 : pt(p1)
nuclear@0 215 {
nuclear@0 216 normal = cross_product(p2 - p1, p3 - p1).normalized();
nuclear@0 217 }
nuclear@0 218
nuclear@0 219 Plane::Plane(const Vector3 &normal, float dist)
nuclear@0 220 {
nuclear@0 221 this->normal = normal.normalized();
nuclear@0 222 pt = this->normal * dist;
nuclear@0 223 }
nuclear@0 224
nuclear@0 225 void Plane::set_union(const GeomObject *obj1, const GeomObject *obj2)
nuclear@0 226 {
nuclear@0 227 fprintf(stderr, "Plane::set_union undefined\n");
nuclear@0 228 }
nuclear@0 229
nuclear@0 230 void Plane::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
nuclear@0 231 {
nuclear@0 232 fprintf(stderr, "Plane::set_intersection undefined\n");
nuclear@0 233 }
nuclear@0 234
nuclear@0 235 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
nuclear@0 236 {
nuclear@0 237 float ndotdir = dot_product(normal, ray.dir);
nuclear@0 238 if(fabs(ndotdir) < 1e-4) {
nuclear@0 239 return false;
nuclear@0 240 }
nuclear@0 241
nuclear@0 242 if(hit) {
nuclear@0 243 Vector3 ptdir = pt - ray.origin;
nuclear@0 244 float t = dot_product(normal, ptdir) / ndotdir;
nuclear@0 245
nuclear@0 246 hit->pos = ray.origin + ray.dir * t;
nuclear@0 247 hit->normal = normal;
nuclear@0 248 hit->obj = this;
nuclear@0 249 }
nuclear@0 250 return true;
nuclear@0 251 }