conworlds

diff src/geom.cc @ 13:283cdfa7dda2

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