3dphotoshoot

diff src/geom.cc @ 25:ac80210d5fbe

preparing a pc version for easier development of non-android-specifics
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Jun 2015 03:12:30 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/geom.cc	Thu Jun 18 03:12:30 2015 +0300
     1.3 @@ -0,0 +1,251 @@
     1.4 +#include <algorithm>
     1.5 +#include <float.h>
     1.6 +#include "geom.h"
     1.7 +
     1.8 +GeomObject::~GeomObject()
     1.9 +{
    1.10 +}
    1.11 +
    1.12 +
    1.13 +Sphere::Sphere()
    1.14 +{
    1.15 +	radius = 1.0;
    1.16 +}
    1.17 +
    1.18 +Sphere::Sphere(const Vector3 &cent, float radius)
    1.19 +	: center(cent)
    1.20 +{
    1.21 +	this->radius = radius;
    1.22 +}
    1.23 +
    1.24 +void Sphere::set_union(const GeomObject *obj1, const GeomObject *obj2)
    1.25 +{
    1.26 +	const Sphere *sph1 = dynamic_cast<const Sphere*>(obj1);
    1.27 +	const Sphere *sph2 = dynamic_cast<const Sphere*>(obj2);
    1.28 +
    1.29 +	if(!sph1 || !sph2) {
    1.30 +		fprintf(stderr, "Sphere::set_union: arguments must be spheres");
    1.31 +		return;
    1.32 +	}
    1.33 +
    1.34 +	float dist = (sph1->center - sph2->center).length();
    1.35 +	float surf_dist = dist - (sph1->radius + sph2->radius);
    1.36 +	float d1 = sph1->radius + surf_dist / 2.0;
    1.37 +	float d2 = sph2->radius + surf_dist / 2.0;
    1.38 +	float t = d1 / (d1 + d2);
    1.39 +
    1.40 +	if(t < 0.0) t = 0.0;
    1.41 +	if(t > 1.0) t = 1.0;
    1.42 +
    1.43 +	center = sph1->center * t + sph2->center * (1.0 - t);
    1.44 +	radius = std::max(dist * t + sph2->radius, dist * (1.0f - t) + sph1->radius);
    1.45 +}
    1.46 +
    1.47 +void Sphere::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
    1.48 +{
    1.49 +	fprintf(stderr, "Sphere::intersection undefined\n");
    1.50 +}
    1.51 +
    1.52 +bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
    1.53 +{
    1.54 +	float a = dot_product(ray.dir, ray.dir);
    1.55 +	float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
    1.56 +		2.0 * ray.dir.y * (ray.origin.y - center.y) +
    1.57 +		2.0 * ray.dir.z * (ray.origin.z - center.z);
    1.58 +	float c = dot_product(ray.origin, ray.origin) + dot_product(center, center) -
    1.59 +		2.0 * dot_product(ray.origin, center) - radius * radius;
    1.60 +
    1.61 +	float discr = b * b - 4.0 * a * c;
    1.62 +	if(discr < 1e-4) {
    1.63 +		return false;
    1.64 +	}
    1.65 +
    1.66 +	float sqrt_discr = sqrt(discr);
    1.67 +	float t0 = (-b + sqrt_discr) / (2.0 * a);
    1.68 +	float t1 = (-b - sqrt_discr) / (2.0 * a);
    1.69 +
    1.70 +	if(t0 < 1e-4)
    1.71 +		t0 = t1;
    1.72 +	if(t1 < 1e-4)
    1.73 +		t1 = t0;
    1.74 +
    1.75 +	float t = t0 < t1 ? t0 : t1;
    1.76 +	if(t < 1e-4) {
    1.77 +		return false;
    1.78 +	}
    1.79 +
    1.80 +	// fill the HitPoint structure
    1.81 +	if(hit) {
    1.82 +		hit->obj = this;
    1.83 +		hit->dist = t;
    1.84 +		hit->pos = ray.origin + ray.dir * t;
    1.85 +		hit->normal = (hit->pos - center) / radius;
    1.86 +	}
    1.87 +	return true;
    1.88 +}
    1.89 +
    1.90 +
    1.91 +AABox::AABox()
    1.92 +{
    1.93 +}
    1.94 +
    1.95 +AABox::AABox(const Vector3 &vmin, const Vector3 &vmax)
    1.96 +	: min(vmin), max(vmax)
    1.97 +{
    1.98 +}
    1.99 +
   1.100 +void AABox::set_union(const GeomObject *obj1, const GeomObject *obj2)
   1.101 +{
   1.102 +	const AABox *box1 = dynamic_cast<const AABox*>(obj1);
   1.103 +	const AABox *box2 = dynamic_cast<const AABox*>(obj2);
   1.104 +
   1.105 +	if(!box1 || !box2) {
   1.106 +		fprintf(stderr, "AABox::set_union: arguments must be AABoxes too\n");
   1.107 +		return;
   1.108 +	}
   1.109 +
   1.110 +	min.x = std::min(box1->min.x, box2->min.x);
   1.111 +	min.y = std::min(box1->min.y, box2->min.y);
   1.112 +	min.z = std::min(box1->min.z, box2->min.z);
   1.113 +
   1.114 +	max.x = std::max(box1->max.x, box2->max.x);
   1.115 +	max.y = std::max(box1->max.y, box2->max.y);
   1.116 +	max.z = std::max(box1->max.z, box2->max.z);
   1.117 +}
   1.118 +
   1.119 +void AABox::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
   1.120 +{
   1.121 +	const AABox *box1 = dynamic_cast<const AABox*>(obj1);
   1.122 +	const AABox *box2 = dynamic_cast<const AABox*>(obj2);
   1.123 +
   1.124 +	if(!box1 || !box2) {
   1.125 +		fprintf(stderr, "AABox::set_intersection: arguments must be AABoxes too\n");
   1.126 +		return;
   1.127 +	}
   1.128 +
   1.129 +	for(int i=0; i<3; i++) {
   1.130 +		min[i] = std::max(box1->min[i], box2->min[i]);
   1.131 +		max[i] = std::min(box1->max[i], box2->max[i]);
   1.132 +
   1.133 +		if(max[i] < min[i]) {
   1.134 +			max[i] = min[i];
   1.135 +		}
   1.136 +	}
   1.137 +}
   1.138 +
   1.139 +bool AABox::intersect(const Ray &ray, HitPoint *hit) const
   1.140 +{
   1.141 +	Vector3 param[2] = {min, max};
   1.142 +	Vector3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
   1.143 +	int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
   1.144 +
   1.145 +	float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
   1.146 +	float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
   1.147 +	float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
   1.148 +	float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
   1.149 +
   1.150 +	if(tmin > tymax || tymin > tmax) {
   1.151 +		return false;
   1.152 +	}
   1.153 +	if(tymin > tmin) {
   1.154 +		tmin = tymin;
   1.155 +	}
   1.156 +	if(tymax < tmax) {
   1.157 +		tmax = tymax;
   1.158 +	}
   1.159 +
   1.160 +	float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
   1.161 +	float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
   1.162 +
   1.163 +	if(tmin > tzmax || tzmin > tmax) {
   1.164 +		return false;
   1.165 +	}
   1.166 +	if(tzmin > tmin) {
   1.167 +		tmin = tzmin;
   1.168 +	}
   1.169 +	if(tzmax < tmax) {
   1.170 +		tmax = tzmax;
   1.171 +	}
   1.172 +
   1.173 +	float t = tmin < 1e-4 ? tmax : tmin;
   1.174 +	if(t >= 1e-4) {
   1.175 +
   1.176 +		if(hit) {
   1.177 +			hit->obj = this;
   1.178 +			hit->dist = t;
   1.179 +			hit->pos = ray.origin + ray.dir * t;
   1.180 +
   1.181 +			float min_dist = FLT_MAX;
   1.182 +			Vector3 offs = min + (max - min) / 2.0;
   1.183 +			Vector3 local_hit = hit->pos - offs;
   1.184 +
   1.185 +			static const Vector3 axis[] = {
   1.186 +				Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)
   1.187 +			};
   1.188 +			//int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
   1.189 +
   1.190 +			for(int i=0; i<3; i++) {
   1.191 +				float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
   1.192 +				if(dist < min_dist) {
   1.193 +					min_dist = dist;
   1.194 +					hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
   1.195 +					//hit->texcoord = Vector2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
   1.196 +				}
   1.197 +			}
   1.198 +		}
   1.199 +		return true;
   1.200 +	}
   1.201 +	return false;
   1.202 +
   1.203 +}
   1.204 +
   1.205 +Plane::Plane()
   1.206 +	: normal(0.0, 1.0, 0.0)
   1.207 +{
   1.208 +}
   1.209 +
   1.210 +Plane::Plane(const Vector3 &p, const Vector3 &norm)
   1.211 +	: pt(p)
   1.212 +{
   1.213 +	normal = norm.normalized();
   1.214 +}
   1.215 +
   1.216 +Plane::Plane(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3)
   1.217 +	: pt(p1)
   1.218 +{
   1.219 +	normal = cross_product(p2 - p1, p3 - p1).normalized();
   1.220 +}
   1.221 +
   1.222 +Plane::Plane(const Vector3 &normal, float dist)
   1.223 +{
   1.224 +	this->normal = normal.normalized();
   1.225 +	pt = this->normal * dist;
   1.226 +}
   1.227 +
   1.228 +void Plane::set_union(const GeomObject *obj1, const GeomObject *obj2)
   1.229 +{
   1.230 +	fprintf(stderr, "Plane::set_union undefined\n");
   1.231 +}
   1.232 +
   1.233 +void Plane::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
   1.234 +{
   1.235 +	fprintf(stderr, "Plane::set_intersection undefined\n");
   1.236 +}
   1.237 +
   1.238 +bool Plane::intersect(const Ray &ray, HitPoint *hit) const
   1.239 +{
   1.240 +	float ndotdir = dot_product(normal, ray.dir);
   1.241 +	if(fabs(ndotdir) < 1e-4) {
   1.242 +		return false;
   1.243 +	}
   1.244 +
   1.245 +	if(hit) {
   1.246 +		Vector3 ptdir = pt - ray.origin;
   1.247 +		float t = dot_product(normal, ptdir) / ndotdir;
   1.248 +
   1.249 +		hit->pos = ray.origin + ray.dir * t;
   1.250 +		hit->normal = normal;
   1.251 +		hit->obj = this;
   1.252 +	}
   1.253 +	return true;
   1.254 +}