chiark / gitweb /
Initial checkin - found in davenant:~ian/chiark/things/moebius
[moebius.git] / library.cc
1 /*
2  * Points library
3  */
4
5 #include <stdlib.h>
6
7 #include "library.hh"
8 #include "transforms.hh"
9
10 double Point::planedistance= 1.0;
11 double Point::eyedistance= 1.0;
12 double Point::cutoffdistance= 10.0;
13 double Point::eyex= 0.0;
14
15 TransformList Point::usertransforms;
16 TransformList Point::povtransform;
17
18 Point TransformList::operator()(Point it) {
19   for (int i=0; i<used; i++) { it= (*a[i])(it); }
20   return it;
21 }
22
23 void TransformList::append(Transform *it) {
24   if (used >= size) {
25     size+=5; size<<=1;
26     a= (Transform**)realloc(a,sizeof(Transform*)*size);
27     if (!a) { perror("realloc"); exit(1); }
28   }
29   a[used++]= it;
30 }
31
32 void TransformList::clearcontents() {
33   for (int i=0; i<used; i++) delete a[i];
34 }
35   
36 Point::operator Onscreen() const {
37   Point it= transformed();
38   double factor= eyedistance / (eyedistance + planedistance - it[2]);
39   return Onscreen(it[0] * factor + eyex * (1.0-factor), it[1] * factor);
40 }
41
42 void Point::setobserver(double theta, double eta, double planedist, double eyedist,
43                         double cutoffdist) {
44   planedistance= planedist;
45   eyedistance= eyedist;
46   cutoffdistance= cutoffdist;
47   povtransform.reset();
48   if (theta != 0.0) povtransform.append(new XZRotationTransform(theta));
49   if (eta != 0.0) povtransform.append(new YZRotationTransform(eta));
50 }