summaryrefslogtreecommitdiffstats
path: root/libdimension-python/dimension.pyx
blob: a34d2eb72439e1148bfff3c44a8edee0cbf9eac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
#########################################################################
# Copyright (C) 2011 Tavian Barnes <tavianator@tavianator.com>          #
#                                                                       #
# This file is part of The Dimension Python Module.                     #
#                                                                       #
# The Dimension Python Module is free software; you can redistribute it #
# and/or modify it under the terms of the GNU Lesser General Public     #
# License as published by the Free Software Foundation; either version  #
# 3 of the License, or (at your option) any later version.              #
#                                                                       #
# The Dimension Python Module is distributed in the hope that it will   #
# be useful, but WITHOUT ANY WARRANTY; without even the implied         #
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See #
# the GNU Lesser General Public License for more details.               #
#                                                                       #
# You should have received a copy of the GNU Lesser General Public      #
# License along with this program.  If not, see                         #
# <http://www.gnu.org/licenses/>.                                       #
#########################################################################

"""
Dimension: a high-performance photo-realistic 3D renderer.
"""

import os

###########
# Helpers #
###########

cdef _raise_OSError(filename = None):
  if filename is None:
    raise OSError(errno, os.strerror(errno))
  else:
    raise OSError(errno, os.strerror(errno), filename)

###########
# Globals #
###########

def die_on_warnings(always_die):
  """Whether to treat Dimension warnings as errors."""
  dmnsn_die_on_warnings(always_die)

def terminal_width():
  """Return the width of the terminal, if present."""
  return dmnsn_terminal_width()

###########
# Futures #
###########

cdef class Future:
  cdef dmnsn_future *_future
  cdef _finalizer

  def __cinit__(self):
    self._future = NULL
    self._finalizer = None

  def __init__(self):
    raise RuntimeError("attempt to create a Future object.")

  def __dealloc__(self):
    if self._future != NULL:
      self.join()

  def join(self):
    self._assert_unfinished()
    cdef int retcode
    try:
      with nogil:
        retcode = dmnsn_future_join(self._future)
      if retcode != 0:
        raise RuntimeError("background task failed.")
      if self._finalizer is not None:
        self._finalizer()
    finally:
      self._future = NULL

  def cancel(self):
    self._assert_unfinished()
    dmnsn_future_cancel(self._future)

  def progress(self):
    self._assert_unfinished()
    return dmnsn_future_progress(self._future)

  def wait(self, progress):
    self._assert_unfinished()
    with nogil:
      dmnsn_future_wait(self._future, progress)

  # Let Futures be used as context managers
  def __enter__(self):
    return self
  def __exit__(self, exc_type, exc_value, traceback):
    if self._future != NULL:
      self.join()
    return False

  def _assert_unfinished(self):
    if self._future == NULL:
      raise RuntimeError("background task finished.")

cdef Future _Future(dmnsn_future *future):
  """Wrap a Future object around an existing dmnsn_future *."""
  cdef Future self = Future.__new__(Future)
  self._future = future
  return self

##########
# Timers #
##########

cdef class Timer:
  """A timer for Dimension tasks."""
  cdef dmnsn_timer _timer
  cdef bool _stopped

  def __init__(self):
    """
    Create a Timer.

    Timing starts as soon as the object is created.
    """
    self._stopped = False
    dmnsn_timer_start(&self._timer)

  def stop(self):
    """Stop the Timer."""
    if self._stopped:
      raise RuntimeError("timer already stopped.")

    dmnsn_timer_stop(&self._timer)
    self._stopped = True

  property real:
    """Real (wall clock) time."""
    def __get__(self):
      self._assert_stopped()
      return self._timer.real
  property user:
    """User (CPU) time."""
    def __get__(self):
      self._assert_stopped()
      return self._timer.user
  property system:
    """System time."""
    def __get__(self):
      self._assert_stopped()
      return self._timer.system

  def __str__(self):
    self._assert_stopped()
    return "%.2fs (user: %.2fs; system: %.2fs)" % \
           (self._timer.real, self._timer.user, self._timer.system)

  def _assert_stopped(self):
    if not self._stopped:
      raise RuntimeError("timer still running.")

cdef Timer _Timer(dmnsn_timer timer):
  """Wrap a Timer object around a dmnsn_timer."""
  cdef Timer self = Timer.__new__(Timer)
  self._timer = timer
  self._stopped = True
  return self

############
# Geometry #
############

cdef class Vector:
  """A vector (or point or pseudovector) in 3D space."""
  cdef dmnsn_vector _v

  def __init__(self, *args, **kwargs):
    """
    Create a Vector.

    Keyword arguments:
    x -- The x coordinate
    y -- The y coordinate
    z -- The z coordinate

    Alternatively, you can pass another Vector, the value 0, or a tuple or other
    sequence (x, y, z).
    """
    if len(args) == 1:
      if isinstance(args[0], Vector):
        self._v = (<Vector>args[0])._v
      elif hasattr(args[0], "__iter__"): # Faster than try: ... except:
        self._real_init(*args[0])
      elif args[0] == 0:
        self._v = dmnsn_zero
      else:
        raise TypeError("expected a sequence or 0")
    else:
      self._real_init(*args, **kwargs)

  def _real_init(self, double x, double y, double z):
    self._v = dmnsn_new_vector(x, y, z)

  property x:
    """The x coordinate."""
    def __get__(self):
      return self._v.x
  property y:
    """The y coordinate."""
    def __get__(self):
      return self._v.y
  property z:
    """The z coordinate."""
    def __get__(self):
      return self._v.z

  def __pos__(self):
    return self
  def __neg__(self):
    return _Vector(dmnsn_vector_negate(self._v))
  def __nonzero__(self):
    return dmnsn_vector_norm(self._v) >= dmnsn_epsilon

  def __add__(lhs, rhs):
    return _Vector(dmnsn_vector_add(Vector(lhs)._v, Vector(rhs)._v))
  def __sub__(lhs, rhs):
    return _Vector(dmnsn_vector_sub(Vector(lhs)._v, Vector(rhs)._v))
  def __mul__(lhs, rhs):
    if isinstance(lhs, Vector):
      return _Vector(dmnsn_vector_mul(rhs, (<Vector>lhs)._v))
    else:
      return _Vector(dmnsn_vector_mul(lhs, (<Vector?>rhs)._v))
  def __truediv__(Vector lhs not None, double rhs):
    return _Vector(dmnsn_vector_div(lhs._v, rhs))

  def __richcmp__(lhs, rhs, int op):
    equal = (Vector(lhs) - Vector(rhs)).norm() < dmnsn_epsilon
    if op == 2:   # ==
      return equal
    elif op == 3: # !=
      return not equal
    else:
      return NotImplemented

  def norm(self):
    """Return the magnitude of the vector."""
    return dmnsn_vector_norm(self._v)
  def normalized(self):
    """Return the direction of the vector."""
    return _Vector(dmnsn_vector_normalized(self._v))

  def __repr__(self):
    return "dimension.Vector(%r, %r, %r)" % (self.x, self.y, self.z)

  def __str__(self):
    return "<%s, %s, %s>" % (self.x, self.y, self.z)

cdef Vector _Vector(dmnsn_vector v):
  """Wrap a Vector object around a dmnsn_vector."""
  cdef Vector self = Vector.__new__(Vector)
  self._v = v
  return self

def cross(Vector lhs not None, Vector rhs not None):
  """Vector cross product."""
  return _Vector(dmnsn_vector_cross(lhs._v, rhs._v))
def dot(Vector lhs not None, Vector rhs not None):
  """Vector dot product."""
  return dmnsn_vector_dot(lhs._v, rhs._v)
def proj(Vector u not None, Vector d not None):
  """Vector projection (of u onto d)."""
  return _Vector(dmnsn_vector_proj(u._v, d._v))

X = _Vector(dmnsn_x)
Y = _Vector(dmnsn_y)
Z = _Vector(dmnsn_z)

cdef class Matrix:
  """An affine transformation matrix."""
  cdef dmnsn_matrix _m

  def __init__(self,
               double a1, double a2, double a3, double a4,
               double b1, double b2, double b3, double b4,
               double c1, double c2, double c3, double c4):
    """Create a Matrix."""
    self._m = dmnsn_new_matrix(a1, a2, a3, a4,
                               b1, b2, b3, b4,
                               c1, c2, c3, c4)

  def __nonzero__(self):
    cdef double sum = 0.0
    for i in range(3):
      for j in range(4):
        sum += self._m.n[i][j]
    return sqrt(sum) >= dmnsn_epsilon

  def __mul__(Matrix lhs not None, rhs):
    if isinstance(rhs, Matrix):
      return _Matrix(dmnsn_matrix_mul(lhs._m, (<Matrix>rhs)._m))
    else:
      return _Vector(dmnsn_transform_point(lhs._m, (<Vector?>rhs)._v))

  def __richcmp__(Matrix lhs not None, Matrix rhs not None, int op):
    cdef double sum = 0.0
    for i in range(3):
      for j in range(4):
        diff = lhs._m.n[i][j] - rhs._m.n[i][j]
        sum += diff*diff
    equal = sqrt(sum) < dmnsn_epsilon

    if op == 2:   # ==
      return equal
    elif op == 3: # !=
      return not equal
    else:
      return NotImplemented

  def inverse(self):
    """Return the inverse of a matrix."""
    return _Matrix(dmnsn_matrix_inverse(self._m))

  def __repr__(self):
    return \
      "dimension.Matrix(%r, %r, %r, %r, %r, %r, %r, %r, %r, %r, %r, %r)" % \
      (self._m.n[0][0], self._m.n[0][1], self._m.n[0][2], self._m.n[0][3],
       self._m.n[1][0], self._m.n[1][1], self._m.n[1][2], self._m.n[1][3],
       self._m.n[2][0], self._m.n[2][1], self._m.n[2][2], self._m.n[2][3])

  def __str__(self):
    return \
      "\n[%s\t%s\t%s\t%s]" \
      "\n[%s\t%s\t%s\t%s]" \
      "\n[%s\t%s\t%s\t%s]" \
      "\n[%s\t%s\t%s\t%s]" %\
      (self._m.n[0][0], self._m.n[0][1], self._m.n[0][2], self._m.n[0][3],
       self._m.n[1][0], self._m.n[1][1], self._m.n[1][2], self._m.n[1][3],
       self._m.n[2][0], self._m.n[2][1], self._m.n[2][2], self._m.n[2][3],
       0.0, 0.0, 0.0, 1.0)

cdef Matrix _Matrix(dmnsn_matrix m):
  """Wrap a Matrix object around a dmnsn_matrix."""
  cdef Matrix self = Matrix.__new__(Matrix)
  self._m = m
  return self

def scale(*args, **kwargs):
  """
  Return a scale transformation.

  Accepts the same arguments that Vector(...) does.  The transformation scales
  by a factor of x in the x direction, y in the y direction, and z in the z
  direction.  In particular, this means that scale(2*X) is probably a mistake,
  as the y and z coordinates will disappear.

  Alternatively, a single argument may be passed, which specifies the scaling
  factor in every component.
  """
  cdef Vector s
  try:
    s = Vector(*args, **kwargs)
  except:
    s = args[0]*(X + Y + Z)
  return _Matrix(dmnsn_scale_matrix(s._v))
def translate(*args, **kwargs):
  """
  Return a translation.

  Accepts the same arguments that Vector(...) does.
  """
  return _Matrix(dmnsn_translation_matrix(Vector(*args, **kwargs)._v))
def rotate(*args, **kwargs):
  """
  Return a rotation.

  Accepts the same arguments that Vector(...) does. theta.norm() is the left-
  handed angle of rotation, and theta.normalized() is the axis of rotation.
  theta is specified in degrees.
  """
  cdef Vector rad = dmnsn_radians(1.0)*Vector(*args, **kwargs)
  return _Matrix(dmnsn_rotation_matrix(rad._v))

cdef class _Transformable:
  def scale(self, *args, **kwargs):
    """Scale.  Equivalent to self.transform(scale(...))."""
    return self.transform(scale(*args, **kwargs))
  def translate(self, *args, **kwargs):
    """Translate.  Equivalent to self.transform(translate(...))."""
    return self.transform(translate(*args, **kwargs))
  def rotate(self, *args, **kwargs):
    """Rotate.  Equivalent to self.transform(rotate(...))."""
    return self.transform(rotate(*args, **kwargs))

##########
# Colors #
##########

cdef class _BaseColor:
  cdef dmnsn_color _c
  cdef dmnsn_color _clin

  def __init__(self, *args, **kwargs):
    """
    Create a color.

    Keyword arguments:
    red    -- The red component
    green  -- The green component
    blue   -- The blue component

    Alternatively, you can pass another Color, a gray intensity like 0.5, or a
    tuple or other sequence (red, green, blue).
    """
    if len(args) == 1 and len(kwargs) == 0:
      if isinstance(args[0], _BaseColor):
        self._clin = (<_BaseColor>args[0])._clin
        self._unlinearize()
        return
      elif hasattr(args[0], "__iter__"):
        self._real_init(*args[0])
      else:
        self._c = dmnsn_color_mul(args[0], dmnsn_white)
    else:
      self._real_init(*args, **kwargs)

    self._linearize()

  def _real_init(self, double red, double green, double blue):
    self._c = dmnsn_new_color(red, green, blue)

  property red:
    """The red component."""
    def __get__(self):
      return self._c.R
  property green:
    """The green component."""
    def __get__(self):
      return self._c.G
  property blue:
    """The blue component."""
    def __get__(self):
      return self._c.B

  def intensity(self):
    return dmnsn_color_intensity(self._c)
  def gray(self):
    return _Color(dmnsn_color_mul(self.intensity(), dmnsn_white), type(self))

  def __add__(lhs, rhs):
    if isinstance(lhs, _BaseColor):
      if isinstance(rhs, _BaseColor):
        if type(lhs) is not type(rhs):
          return NotImplemented
      else:
        rhs = type(lhs)(rhs)
    elif isinstance(rhs, _BaseColor):
      lhs = type(rhs)(lhs)
    else:
      return NotImplemented

    return _Color(dmnsn_color_add((<_BaseColor>lhs)._c, (<_BaseColor>rhs)._c),
                  type(lhs))

  def __mul__(lhs, rhs):
    if isinstance(lhs, _BaseColor):
      return _Color(dmnsn_color_mul(rhs, (<_BaseColor>lhs)._c), type(lhs))
    else:
      return _Color(dmnsn_color_mul(lhs, (<_BaseColor?>rhs)._c), type(rhs))

  def __truediv__(_BaseColor lhs not None, double rhs):
    return _Color(dmnsn_color_mul(1/rhs, lhs._c), type(lhs))

  def __richcmp__(lhs, rhs, int op):
    cdef clhs = Color(lhs)
    cdef crhs = Color(rhs)

    cdef double rdiff = clhs.red    - crhs.red
    cdef double gdiff = clhs.green  - crhs.green
    cdef double bdiff = clhs.blue   - crhs.blue
    cdef double sum = rdiff*rdiff + gdiff*gdiff + bdiff*bdiff
    equal = sqrt(sum) < dmnsn_epsilon
    if op == 2:   # ==
      return equal
    elif op == 3: # !=
      return not equal
    else:
      return NotImplemented

  def __repr__(self):
    return "dimension.%s(%r, %r, %r)" % \
           (type(self).__name__, self.red, self.green, self.blue)
  def __str__(self):
    return "%s<%s, %s, %s>" % \
           (type(self).__name__, self.red, self.green, self.blue)

cdef class Color(_BaseColor):
  """
  An object or light color.

  These colors are in a linear RGB space.  For colors in the sRGB space, which
  is used by the web and computer displays, see the sRGB class.
  """

  def _linearize(self):
    self._clin = self._c
  def _unlinearize(self):
    self._c = self._clin

cdef class sRGB(_BaseColor):
  """
  An sRGB color.

  Color operations with these colors occur in sRGB space, which is used by the
  web and computer displays.  However, it is not linear, so (for example) two
  lights with intensity sRGB(0.5) is not the same as one light with intensity
  sRGB(1).
  """

  def _linearize(self):
    self._clin = dmnsn_color_from_sRGB(self._c)
  def _unlinearize(self):
    self._c = dmnsn_color_to_sRGB(self._clin)

cdef _BaseColor _Color(dmnsn_color c, type t):
  """Wrap a _BaseColor subclass around a dmnsn_color."""
  cdef _BaseColor self = t.__new__(t)
  self._c = c
  self._linearize()
  return self

Black   = _Color(dmnsn_black, Color)
White   = _Color(dmnsn_white, Color)
Red     = _Color(dmnsn_red, Color)
Green   = _Color(dmnsn_green, Color)
Blue    = _Color(dmnsn_blue, Color)
Magenta = _Color(dmnsn_magenta, Color)
Orange  = _Color(dmnsn_orange, Color)
Yellow  = _Color(dmnsn_yellow, Color)
Cyan    = _Color(dmnsn_cyan, Color)

cdef class TColor:
  """
  A transparent color.

  This type is used for representing pigments and pixels, as it carries color as
  well as transparency information.
  """
  cdef dmnsn_tcolor _tc

  def __init__(self, *args, **kwargs):
    """
    Create a transparent color.

    Keyword arguments:
    color  -- The Color() (or sRGB()) component
    trans  -- The transparency component
    filter -- The proportion of the transparency that is filtered

    Alternatively, you can pass another TColor.
    """
    if len(args) == 1 and len(kwargs) == 0:
      if isinstance(args[0], TColor):
        self._tc = (<TColor>args[0])._tc
      else:
        self._real_init(*args, **kwargs)
    else:
      self._real_init(*args, **kwargs)

  def _real_init(self, color, double trans = 0, double filter = 0):
    self._tc = dmnsn_new_tcolor(Color(color)._clin, trans, filter)

  property color:
    """The color component."""
    def __get__(self):
      return _Color(self._tc.c, Color)
  property trans:
    """The transparency component."""
    def __get__(self):
      return self._tc.T
  property filter:
    """The filter proportion."""
    def __get__(self):
      return self._tc.F

  def __repr__(self):
    return "dimension.TColor(%r, %r, %r)" % \
           (self.color, self.trans, self.filter)
  def __str__(self):
    return "TColor<%s, %s, %s>" % \
           (self.color, self.trans, self.filter)

cdef TColor _TColor(dmnsn_tcolor tc):
  """Wrap a TColor around a dmnsn_tcolor."""
  cdef TColor self = TColor.__new__(TColor)
  self._tc = tc
  return self

Clear = _TColor(dmnsn_clear)

############
# Canvases #
############

cdef class Canvas:
  """A rendering target."""
  cdef dmnsn_canvas *_canvas

  def __init__(self, width, height):
    """
    Create a Canvas.

    Keyword arguments:
    width  -- the width of the canvas
    height -- the height of the canvas
    """
    self._canvas = dmnsn_new_canvas(width, height)
    self.clear(Black)

  def __dealloc__(self):
    dmnsn_delete_canvas(self._canvas)

  property width:
    """The width of the canvas."""
    def __get__(self):
      return self._canvas.width
  property height:
    """The height of the canvas."""
    def __get__(self):
      return self._canvas.height

  def __len__(self):
    """The width of the canvas."""
    return self.width
  def __getitem__(self, int x):
    """Get a column of the canvas."""
    if x < 0 or x >= self.width:
      raise IndexError("x coordinate out of bounds.")
    return _CanvasProxy(self, x)

  def optimize_PNG(self):
    """Optimize a canvas for PNG output."""
    if dmnsn_png_optimize_canvas(self._canvas) != 0:
      _raise_OSError()

  def optimize_GL(self):
    """Optimize a canvas for OpenGL output."""
    if dmnsn_gl_optimize_canvas(self._canvas) != 0:
      _raise_OSError()

  def clear(self, c):
    """Clear a canvas with a solid color."""
    dmnsn_canvas_clear(self._canvas, TColor(c)._tc)

  def write_PNG(self, path):
    """Export the canvas as a PNG file."""
    self.write_PNG_async(path).join()
  def write_PNG_async(self, path):
    """Export the canvas as a PNG file, in the background."""
    bpath = path.encode("UTF-8")
    cdef char *cpath = bpath
    cdef FILE *file = fopen(cpath, "wb")
    if file == NULL:
      _raise_OSError(path)

    def finalize():
      if fclose(file) != 0:
        _raise_OSError()

    cdef dmnsn_future *future = dmnsn_png_write_canvas_async(self._canvas, file)

    try:
      if future == NULL:
        _raise_OSError()

      ret = _Future(future)
      ret._finalizer = finalize
      return ret
    except:
      finalize()
      raise

  def draw_GL(self):
    """Export the canvas to the current OpenGL context."""
    if dmnsn_gl_write_canvas(self._canvas) != 0:
      _raise_OSError()

cdef class _CanvasProxy:
  cdef dmnsn_canvas *_canvas
  cdef int _x

  def __init__(self, Canvas canvas not None, int x):
    self._canvas = canvas._canvas
    self._x = x

  def __len__(self):
    """The height of the canvas."""
    return self._canvas.height
  def __getitem__(self, int y):
    self._bounds_check(y)
    return _TColor(dmnsn_canvas_get_pixel(self._canvas, self._x, y))
  def __setitem__(self, int y, color):
    self._bounds_check(y)
    dmnsn_canvas_set_pixel(self._canvas, self._x, y, TColor(color)._tc)

  def _bounds_check(self, int y):
    if y < 0 or y >= self._canvas.height:
      raise IndexError("y coordinate out of bounds.")

############
# Patterns #
############

cdef class Pattern:
  """A function which maps points in 3D space to scalar values."""
  cdef dmnsn_pattern *_pattern

  def __cinit__(self):
    self._pattern = NULL

  def __dealloc__(self):
    dmnsn_delete_pattern(self._pattern)

cdef class Checker(Pattern):
  """A checkerboard pattern."""
  def __init__(self):
    self._pattern = dmnsn_new_checker_pattern()
    Pattern.__init__(self)

cdef class Gradient(Pattern):
  """A gradient pattern."""
  def __init__(self, orientation):
    """
    Create a gradient pattern.

    Keyword arguments:
    orientation -- The direction of the linear gradient.
    """
    self._pattern = dmnsn_new_gradient_pattern(Vector(orientation)._v)
    Pattern.__init__(self)

cdef class Leopard(Pattern):
  """A leopard pattern."""
  def __init__(self):
    self._pattern = dmnsn_new_leopard_pattern()
    Pattern.__init__(self)

############
# Pigments #
############

cdef class Pigment(_Transformable):
  """Object surface coloring."""
  cdef dmnsn_pigment *_pigment

  def __cinit__(self):
    self._pigment = NULL

  def __init__(self, quick_color = None):
    """
    Create a Pigment.

    With an arguement, create a solid pigment of that color.  Otherwise, create
    a base Pigment.

    Keyword arguments:
    quick_color  -- the object's quick color for low-quality renders
    """
    if quick_color is not None:
      if self._pigment == NULL:
        if isinstance(quick_color, Pigment):
          self._pigment = (<Pigment>quick_color)._pigment
          DMNSN_INCREF(self._pigment)
        else:
          self._pigment = dmnsn_new_solid_pigment(TColor(quick_color)._tc)
      else:
        self._pigment.quick_color = TColor(quick_color)._tc

  def __dealloc__(self):
    dmnsn_delete_pigment(self._pigment)

  def transform(self, Matrix trans not None):
    """Transform a pigment."""
    self._pigment.trans = dmnsn_matrix_mul(trans._m, self._pigment.trans)
    return self

cdef Pigment _Pigment(dmnsn_pigment *pigment):
  """Wrap a Pigment object around a dmnsn_pigment *."""
  cdef Pigment self = Pigment.__new__(Pigment)
  self._pigment = pigment
  DMNSN_INCREF(self._pigment)
  return self

cdef class ImageMap(Pigment):
  """An image-mapped pigment."""
  def __init__(self, path, *args, **kwargs):
    """
    Create an ImageMap.

    Keyword arguments:
    path -- the path of the PNG file to open
    """
    bpath = path.encode("UTF-8")
    cdef char *cpath = bpath
    cdef FILE *file = fopen(cpath, "rb")
    if file == NULL:
      _raise_OSError(path)
    cdef dmnsn_canvas *canvas = dmnsn_png_read_canvas(file)
    if canvas == NULL:
      _raise_OSError(path)
    if fclose(file) != 0:
      _raise_OSError()

    self._pigment = dmnsn_new_canvas_pigment(canvas)
    Pigment.__init__(self, *args, **kwargs)

cdef class PigmentMap(Pigment):
  """A pigment map."""
  def __init__(self, Pattern pattern not None, map, bool sRGB not None = True,
               *args, **kwargs):
    """
    Create a PigmentMap.

    Keyword arguments:
    pattern -- the pattern to use for the mapping
    map     -- a dictionary of the form { val1: color1, val2: pigment2, ... },
               or a list of the form [color1, pigment2, ...]
    sRGB    -- whether the gradients should be in sRGB or linear space
               (default True)
    """
    cdef dmnsn_map *pigment_map = dmnsn_new_pigment_map()
    cdef dmnsn_pigment *real_pigment
    if hasattr(map, "items"):
      for i, pigment in map.items():
        pigment = Pigment(pigment)
        real_pigment = (<Pigment>pigment)._pigment
        DMNSN_INCREF(real_pigment)
        dmnsn_map_add_entry(pigment_map, i, &real_pigment)
    else:
      for i, pigment in enumerate(map):
        pigment = Pigment(pigment)
        real_pigment = (<Pigment>pigment)._pigment
        DMNSN_INCREF(real_pigment)
        dmnsn_map_add_entry(pigment_map, i/len(map), &real_pigment)

    cdef dmnsn_pigment_map_flags flags
    if sRGB:
      flags = DMNSN_PIGMENT_MAP_SRGB
    else:
      flags = DMNSN_PIGMENT_MAP_REGULAR

    DMNSN_INCREF(pattern._pattern)
    self._pigment = dmnsn_new_pigment_map_pigment(pattern._pattern, pigment_map,
                                                  flags)
    Pigment.__init__(self, *args, **kwargs)

############
# Finishes #
############

cdef class Finish:
  """Object surface qualities."""
  cdef dmnsn_finish _finish

  def __cinit__(self):
    self._finish = dmnsn_new_finish()

  def __dealloc__(self):
    dmnsn_delete_finish(self._finish)

  def __add__(Finish lhs not None, Finish rhs not None):
    """
    Combine two finishes.

    In lhs + rhs, the attributes of rhs override those of lhs if any conflict;
    thus, Ambient(0.1) + Ambient(0.2) is the same as Ambient(0.2)
    """
    cdef Finish ret = Finish()
    dmnsn_finish_cascade(&lhs._finish, &ret._finish)
    dmnsn_finish_cascade(&rhs._finish, &ret._finish) # rhs gets priority
    return ret

cdef Finish _Finish(dmnsn_finish finish):
  """Wrap a Finish object around a dmnsn_finish."""
  cdef Finish self = Finish.__new__(Finish)
  self._finish = finish
  dmnsn_finish_incref(&self._finish)
  return self

cdef class Ambient(Finish):
  """Ambient light reflected."""
  def __init__(self, color):
    """
    Create an Ambient finish.

    Keyword arguments:
    color -- the color and intensity of the ambient light
    """
    self._finish.ambient = dmnsn_new_ambient(Color(color)._c)

cdef class Diffuse(Finish):
  """Lambertian diffuse reflection."""
  def __init__(self, diffuse):
    """
    Create a Diffuse finish.

    Keyword arguments:
    diffuse -- the intensity of the diffuse reflection
    """
    self._finish.diffuse = dmnsn_new_lambertian(Color(diffuse).intensity())

cdef class Phong(Finish):
  """Phong specular highlight."""
  def __init__(self, strength, double size = 40.0):
    """
    Create a Phong highlight.

    Keyword arguments:
    strength -- the strength of the Phong highlight
    size -- the "shininess" of the material
    """
    self._finish.specular = dmnsn_new_phong(Color(strength).intensity(), size)

cdef class Reflection(Finish):
  """Reflective finish."""
  def __init__(self, min, max = None, double falloff = 1.0):
    """
    Create a Reflection.

    Keyword arguments:
    min     -- color and intensity of reflection at indirect angles
    max     -- color and intensity of reflection at direct angles (default: min)
    falloff -- exponent for reflection falloff (default: 1.0)
    """
    if max is None:
      max = min

    self._finish.reflection = dmnsn_new_basic_reflection(Color(min)._c,
                                                         Color(max)._c,
                                                         falloff)

############
# Textures #
############

cdef class Texture(_Transformable):
  """Object surface properties."""
  cdef dmnsn_texture *_texture

  def __init__(self, pigment = None, Finish finish = None):
    """
    Create a Texture.

    Keyword arguments:
    pigment -- the Pigment for the texture, or a color (default: None)
    finish  -- the Finish for the texture (default: None)
    """
    self._texture = dmnsn_new_texture()

    if pigment is not None:
      self.pigment = Pigment(pigment)

    if finish is not None:
      self.finish = finish

  def __dealloc__(self):
    dmnsn_delete_texture(self._texture)

  property pigment:
    """The texture's pigment."""
    def __get__(self):
      if self._texture.pigment == NULL:
        return None
      else:
        return _Pigment(self._texture.pigment)
    def __set__(self, pigment):
      dmnsn_delete_pigment(self._texture.pigment)
      cdef Pigment real_pigment
      if pigment is None:
        self._texture.pigment = NULL
      else:
        real_pigment = Pigment(pigment)
        self._texture.pigment = real_pigment._pigment
        DMNSN_INCREF(self._texture.pigment)

  property finish:
    """The texture's finish."""
    def __get__(self):
      return _Finish(self._texture.finish)
    def __set__(self, Finish finish not None):
      dmnsn_delete_finish(self._texture.finish)
      self._texture.finish = finish._finish
      dmnsn_finish_incref(&self._texture.finish)

  def transform(self, Matrix trans not None):
    """Transform a texture."""
    self._texture.trans = dmnsn_matrix_mul(trans._m, self._texture.trans)
    return self

cdef Texture _Texture(dmnsn_texture *texture):
  """Wrap a Texture object around a dmnsn_texture *."""
  cdef Texture self = Texture.__new__(Texture)
  self._texture = texture
  DMNSN_INCREF(self._texture)
  return self

#############
# Interiors #
#############

cdef class Interior:
  """Object interior properties."""
  cdef dmnsn_interior *_interior

  def __init__(self, double ior = 1.0):
    """
    Create an Interior.

    Keyword arguments:
    ior -- index of reflection
    """
    self._interior = dmnsn_new_interior()
    self._interior.ior = ior

  def __dealloc__(self):
    dmnsn_delete_interior(self._interior)

  property ior:
    """Index of reflection."""
    def __get__(self):
      return self._interior.ior
    def __set__(self, double ior):
      self._interior.ior = ior

cdef Interior _Interior(dmnsn_interior *interior):
  """Wrap an Interior object around a dmnsn_interior *."""
  cdef Interior self = Interior.__new__(Interior)
  self._interior = interior
  DMNSN_INCREF(self._interior)
  return self

###########
# Objects #
###########

cdef class Object(_Transformable):
  """Physical objects."""
  cdef dmnsn_object *_object

  def __cinit__(self):
    self._object = NULL

  def __init__(self, Texture texture = None, pigment = None,
               Finish finish = None, Interior interior = None):
    """
    Initialize an Object.

    Keyword arguments:
    texture  -- the object's Texture
    pigment  -- shorthand for specifying the texture's pigment
    finish   -- shorthand for specifying the texture's finish
    interior -- the object's Interior
    """
    if self._object == NULL:
      raise TypeError("attempt to initialize base Object")

    self.texture = texture
    if pigment is not None:
      if texture is not None:
        raise TypeError("both texture and pigment specified.")
      else:
        if self.texture is None:
          self.texture = Texture()
        self.texture.pigment = pigment

    if finish is not None:
      if texture is not None:
        raise TypeError("both texture and finish specified.")
      else:
        if self.texture is None:
          self.texture = Texture()
        self.texture.finish = finish

    if interior is not None:
      self.interior = interior

  def __dealloc__(self):
    dmnsn_delete_object(self._object)

  property texture:
    """The object's Texture."""
    def __get__(self):
      if self._object.texture == NULL:
        return None
      else:
        return _Texture(self._object.texture)
    def __set__(self, Texture texture):
      dmnsn_delete_texture(self._object.texture)
      if texture is None:
        self._object.texture = NULL
      else:
        self._object.texture = texture._texture
        DMNSN_INCREF(self._object.texture)

  property interior:
    """The object's Interior."""
    def __get__(self):
      return _Interior(self._object.interior)
    def __set__(self, Interior interior not None):
      self._object.interior = interior._interior
      DMNSN_INCREF(self._object.interior)

  def transform(self, Matrix trans not None):
    """Transform an object."""
    self._object.trans = dmnsn_matrix_mul(trans._m, self._object.trans)
    return self

  # Transform an object without affecting the texture
  cdef _intrinsic_transform(self, Matrix trans):
    self._object.intrinsic_trans = dmnsn_matrix_mul(
      trans._m,
      self._object.intrinsic_trans
    )

cdef class Triangle(Object):
  """A triangle."""
  def __init__(self, a, b, c, a_normal = None, b_normal = None, c_normal = None,
               *args, **kwargs):
    """
    Create a Triangle.

    Keyword arguments:
    a, b, c                      -- the corners of the triangle
    a_normal, b_normal, c_normal -- the optional normal vectors at those corners

    Additionally, Triangle() accepts any arguments that Object() accepts.
    """
    if a_normal is None and b_normal is None and c_normal is None:
      a_normal = cross(b - a, c - a)
      b_normal = a_normal
      c_normal = a_normal
    self._object = dmnsn_new_triangle(Vector(a)._v, Vector(b)._v, Vector(c)._v,
                                      Vector(a_normal)._v,
                                      Vector(b_normal)._v,
                                      Vector(c_normal)._v)
    Object.__init__(self, *args, **kwargs)

cdef class Plane(Object):
  """A plane."""
  def __init__(self, normal, double distance, *args, **kwargs):
    """
    Create a Plane.

    Keyword arguments:
    normal   -- a vector perpendicular to the plane
    distance -- the distance from the origin to the plane, in the direction of
                normal

    Additionally, Plane() accepts any arguments that Object() accepts.
    """
    self._object = dmnsn_new_plane(Vector(normal)._v)
    Object.__init__(self, *args, **kwargs)

    self._intrinsic_transform(translate(distance*Vector(normal)))

cdef class Sphere(Object):
  """A sphere."""
  def __init__(self, center, double radius, *args, **kwargs):
    """
    Create a Sphere.

    Keyword arguments:
    center -- the center of the sphere
    radius -- the radius of the sphere

    Additionally, Sphere() accepts any arguments that Object() accepts.
    """
    self._object = dmnsn_new_sphere()
    Object.__init__(self, *args, **kwargs)

    cdef Matrix trans = translate(Vector(center))
    trans *= scale(radius, radius, radius)
    self._intrinsic_transform(trans)

cdef class Box(Object):
  """An axis-aligned rectangular prism."""
  def __init__(self, min, max, *args, **kwargs):
    """
    Create a Box.

    Keyword arguments:
    min -- the coordinate-wise minimal extent of the box
    max -- the coordinate-wise maximal extent of the box

    Additionally, Box() accepts any arguments that Object() accepts.
    """
    self._object = dmnsn_new_cube()
    Object.__init__(self, *args, **kwargs)

    min = Vector(min)
    max = Vector(max)
    cdef Matrix trans = translate((max + min)/2)
    trans *= scale((max - min)/2)
    self._intrinsic_transform(trans)

cdef class Cone(Object):
  """A cone or cone slice."""
  def __init__(self, bottom, double bottom_radius, top, double top_radius = 0.0,
               bool open not None = False, *args, **kwargs):
    """
    Create a Cone.

    Keyword arguments:
    bottom        -- the location of the bottom of the cone
    bottom_radius -- the radius at the bottom of the cone
    top           -- the location of the top of the cone
    top_radius    -- the radius at the top of the cone/cone slice (default 0.0)
    open          -- whether to draw the cone cap(s)

    Additionally, Cone() accepts any arguments that Object() accepts.
    """
    self._object = dmnsn_new_cone(bottom_radius, top_radius, open)
    Object.__init__(self, *args, **kwargs)

    # Lift the cone to start at the origin, then scale, rotate, and translate
    # properly

    cdef Vector dir = Vector(top) - Vector(bottom)

    cdef Matrix trans = translate(Y)
    trans = scale(1.0, dir.norm()/2, 1.0)*trans
    trans = _Matrix(dmnsn_alignment_matrix(dmnsn_y, dir._v, dmnsn_x, dmnsn_z))*trans
    trans = translate(bottom)*trans

    self._intrinsic_transform(trans)

cdef class Cylinder(Cone):
  """A cylinder."""
  def __init__(self, bottom, top, double radius, bool open not None = False,
               *args, **kwargs):
    """
    Create a Cylinder.

    Keyword arguments:
    bottom  -- the location of the bottom of the cylinder
    top     -- the location of the top of the cylinder
    radius  -- the radius of the cylinder
    open    -- whether to draw the cylinder caps

    Additionally, Cylinder() accepts any arguments that Object() accepts.
    """
    Cone.__init__(self,
                  bottom = bottom, bottom_radius = radius,
                  top    = top,    top_radius    = radius,
                  open = open,
                  *args, **kwargs)

cdef class Torus(Object):
  """A torus."""
  def __init__(self, double major_radius, double minor_radius, *args, **kwargs):
    """
    Create a Torus.

    Keyword arguments:
    major_radius -- the distance from the center of the torus to the center of
                    a circular cross-section of the torus
    minor_radius -- the radius of the circular cross-sections of the torus

    Additionally, Torus() accepts any arguments that Object() accepts.
    """
    self._object = dmnsn_new_torus(major_radius, minor_radius)
    Object.__init__(self, *args, **kwargs)

cdef class Union(Object):
  """A CSG union."""
  def __init__(self, objects, *args, **kwargs):
    """
    Create a Union.

    Keyword arguments:
    objects -- a list of objects to include in the union

    Additionally, Union() accepts any arguments that Object() accepts.
    """
    if len(objects) < 1:
      raise TypeError("expected a list of one or more Objects")

    cdef dmnsn_array *array = dmnsn_new_array(sizeof(dmnsn_object *))
    cdef dmnsn_object *o

    try:
      for obj in objects:
        o = (<Object?>obj)._object
        DMNSN_INCREF(o)
        dmnsn_array_push(array, &o)

      self._object = dmnsn_new_csg_union(array)
    finally:
      dmnsn_delete_array(array)

    Object.__init__(self, *args, **kwargs)

cdef class Intersection(Object):
  """A CSG intersection."""
  def __init__(self, objects, *args, **kwargs):
    """
    Create an Intersection.

    Keyword arguments:
    objects -- a list of objects to include in the intersection

    Additionally, Intersection() accepts any arguments that Object() accepts.
    """
    if len(objects) < 1:
      raise TypeError("expected a list of one or more Objects")

    cdef dmnsn_object *o

    for obj in objects:
      if self._object == NULL:
        self._object = (<Object?>obj)._object
        DMNSN_INCREF(self._object)
      else:
        o = (<Object?>obj)._object
        DMNSN_INCREF(o)
        self._object = dmnsn_new_csg_intersection(self._object, o)

    Object.__init__(self, *args, **kwargs)

cdef class Difference(Object):
  """A CSG difference."""
  def __init__(self, objects, *args, **kwargs):
    """
    Create a Difference.

    Keyword arguments:
    objects -- a list of objects to include in the difference

    Additionally, Difference() accepts any arguments that Object() accepts.
    """
    if len(objects) < 1:
      raise TypeError("expected a list of one or more Objects")

    cdef dmnsn_object *o

    for obj in objects:
      if self._object == NULL:
        self._object = (<Object?>obj)._object
        DMNSN_INCREF(self._object)
      else:
        o = (<Object?>obj)._object
        DMNSN_INCREF(o)
        self._object = dmnsn_new_csg_difference(self._object, o)

    Object.__init__(self, *args, **kwargs)

cdef class Merge(Object):
  """A CSG merge."""
  def __init__(self, objects, *args, **kwargs):
    """
    Create a Merge.

    Keyword arguments:
    objects -- a list of objects to include in the merge

    Additionally, Merge() accepts any arguments that Object() accepts.
    """
    if len(objects) < 1:
      raise TypeError("expected a list of one or more Objects")

    cdef dmnsn_object *o

    for obj in objects:
      if self._object == NULL:
        self._object = (<Object?>obj)._object
        DMNSN_INCREF(self._object)
      else:
        o = (<Object?>obj)._object
        DMNSN_INCREF(o)
        self._object = dmnsn_new_csg_merge(self._object, o)

    Object.__init__(self, *args, **kwargs)

##########
# Lights #
##########

cdef class Light:
  """A light."""
  cdef dmnsn_light *_light

  def __dealloc__(self):
    dmnsn_delete_light(self._light)

cdef class PointLight(Light):
  """A point light."""
  def __init__(self, location, color):
    """
    Create a PointLight.

    Keyword arguments:
    location -- the origin of the light rays
    color    -- the color and intensity of the light
    """
    self._light = dmnsn_new_point_light(Vector(location)._v, Color(color)._c)
    Light.__init__(self)

###########
# Cameras #
###########

cdef class Camera(_Transformable):
  """A camera."""
  cdef dmnsn_camera *_camera

  def __cinit__(self):
    self._camera = NULL

  def __init__(self):
    if self._camera == NULL:
      raise TypeError("attempt to initialize base Camera")

  def __dealloc__(self):
    dmnsn_delete_camera(self._camera)

  def transform(self, Matrix trans not None):
    """Transform a camera."""
    self._camera.trans = dmnsn_matrix_mul(trans._m, self._camera.trans)
    return self

cdef class PerspectiveCamera(Camera):
  """A regular perspective camera."""
  def __init__(self, location = -Z, look_at = 0, sky = Y,
               angle = dmnsn_degrees(atan(1.0))):
    """
    Create a PerspectiveCamera.

    Keyword arguments:
    location -- the location of the camera (default: -Z)
    look_at  -- where to aim the camera (default: 0)
    sky      -- the direction of the top of the camera (default: Y)
    angle    -- the field of view angle (from bottom to top) (default: 45)
    """
    self._camera = dmnsn_new_perspective_camera()
    Camera.__init__(self)

    # Apply the field of view angle
    self.scale(tan(dmnsn_radians(angle))*(X + Y) + Z)

    cdef Vector dir = Vector(look_at) - Vector(location)
    cdef Vector vsky = Vector(sky)

    # Line up the top of the viewport with the sky vector
    cdef Matrix align_sky = _Matrix(dmnsn_alignment_matrix(dmnsn_y, vsky._v,
                                                           dmnsn_z, dmnsn_x))
    cdef Vector forward = align_sky*Z
    cdef Vector right   = align_sky*X

    # Line up the look at point with look_at
    self.transform(_Matrix(dmnsn_alignment_matrix(forward._v, dir._v,
                                                  vsky._v, right._v)))

    # Move the camera into position
    self.translate(Vector(location))

##########
# Scenes #
##########

cdef class Scene:
  """An entire scene."""
  cdef dmnsn_scene *_scene

  def __init__(self, Canvas canvas not None, objects, lights,
               Camera camera not None):
    """
    Create a Scene.

    Keyword arguments:
    canvas  -- the rendering Canvas
    objects -- the list of objects in the scene
    lights  -- the list of lights in the scene
    camera  -- the camera for the scene
    """
    self._scene = dmnsn_new_scene()

    self._scene.canvas = canvas._canvas
    DMNSN_INCREF(self._scene.canvas)
    self.outer_width = self._scene.canvas.width
    self.outer_height = self._scene.canvas.height
    self.background = Black

    cdef dmnsn_object *o
    for obj in objects:
      o = (<Object?>obj)._object
      DMNSN_INCREF(o)
      dmnsn_array_push(self._scene.objects, &o)

    cdef dmnsn_light *l
    for light in lights:
      l = (<Light?>light)._light
      DMNSN_INCREF(l)
      dmnsn_array_push(self._scene.lights, &l)

    self._scene.camera = camera._camera
    DMNSN_INCREF(self._scene.camera)

  # Subregion render support
  property region_x:
    """The x-coordinate of the subregion in the broader image."""
    def __get__(self):
      return self._scene.region_x
    def __set__(self, x):
      self._scene.region_x = x
  property region_y:
    """The y-coordinate of the subregion in the broader image."""
    def __get__(self):
      return self._scene.region_y
    def __set__(self, y):
      self._scene.region_y = y
  property outer_width:
    """The width of the broader image."""
    def __get__(self):
      return self._scene.outer_width
    def __set__(self, width):
      self._scene.outer_width = width
  property outer_height:
    """The height of the broader image."""
    def __get__(self):
      return self._scene.outer_height
    def __set__(self, height):
      self._scene.outer_height = height

  property default_texture:
    """The default Texture for objects."""
    def __get__(self):
      return _Texture(self._scene.default_texture)
    def __set__(self, Texture texture not None):
      dmnsn_delete_texture(self._scene.default_texture)
      self._scene.default_texture = texture._texture
      DMNSN_INCREF(self._scene.default_texture)
  property default_interior:
    """The default Interior for objects."""
    def __get__(self):
      return _Interior(self._scene.default_interior)
    def __set__(self, Interior interior not None):
      dmnsn_delete_interior(self._scene.default_interior)
      self._scene.default_interior = interior._interior
      DMNSN_INCREF(self._scene.default_interior)

  property background:
    """The background pigment of the scene (default: Black)."""
    def __get__(self):
      return _Pigment(self._scene.background)
    def __set__(self, pigment):
      dmnsn_delete_pigment(self._scene.background)
      cdef Pigment real_pigment = Pigment(pigment)
      self._scene.background = real_pigment._pigment
      DMNSN_INCREF(self._scene.background)

  property adc_bailout:
    """The adaptive depth control bailout (default: 1/255)."""
    def __get__(self):
      return self._scene.adc_bailout
    def __set__(self, double bailout):
      self._scene.adc_bailout = bailout

  property recursion_limit:
    """The rendering recursion limit (default: 5)."""
    def __get__(self):
      return self._scene.reclimit
    def __set__(self, level):
      self._scene.reclimit = level

  property nthreads:
    """The number of threads to use for the render."""
    def __get__(self):
      return self._scene.nthreads
    def __set__(self, n):
      if n <= 0:
        raise ValueError("%d is an invalid thread count." % n)
      self._scene.nthreads = n

  property quality:
    """The render quality."""
    def __get__(self):
      return _quality_to_string(self._scene.quality)
    def __set__(self, q):
      self._scene.quality = _string_to_quality(q)

  property bounding_timer:
    """The Timer for building the bounding hierarchy."""
    def __get__(self):
      return _Timer(self._scene.bounding_timer)
  property render_timer:
    """The Timer for the actual render."""
    def __get__(self):
      return _Timer(self._scene.render_timer)

  def ray_trace(self):
    """Render the scene."""
    self.ray_trace_async().join()
  def ray_trace_async(self):
    """Render the scene, in the background."""
    # Account for image dimensions in the camera
    # Do this here so subregion renders can tell us the broader image size
    self._scene.camera.trans = dmnsn_matrix_mul(
      self._scene.camera.trans,
      dmnsn_scale_matrix(
        dmnsn_new_vector(
          self.outer_width/self.outer_height,
          1.0,
          1.0
        )
      )
    )
    # Ensure the default texture is complete
    cdef Texture default = Texture(pigment = Black)
    dmnsn_texture_cascade(default._texture, &self._scene.default_texture)
    return _Future(dmnsn_ray_trace_async(self._scene))

  def __dealloc__(self):
    dmnsn_delete_scene(self._scene)

def _quality_to_string(int quality):
  cdef str s = ""

  if quality & DMNSN_RENDER_PIGMENT:
    s += 'p'
  if quality & DMNSN_RENDER_LIGHTS:
    s += 'l'
  if quality & DMNSN_RENDER_FINISH:
    s += 'f'
  if quality & DMNSN_RENDER_TRANSPARENCY:
    s += 't'
  if quality & DMNSN_RENDER_REFLECTION:
    s += 'r'

  if s == "":
    return "0"
  else:
    return s

def _string_to_quality(str quality not None):
  cdef int q = DMNSN_RENDER_NONE
  inverse = False

  if quality == "":
    return q

  if quality[0] == '^':
    inverse = True
    quality = quality[1:]

  if quality != "0":
    while len(quality) > 0:
      ch = quality[0]
      quality = quality[1:]

      if ch == 'p':
        flag = DMNSN_RENDER_PIGMENT
      elif ch == 'l':
        flag = DMNSN_RENDER_LIGHTS
      elif ch == 'f':
        flag = DMNSN_RENDER_FINISH
      elif ch == 't':
        flag = DMNSN_RENDER_TRANSPARENCY
      elif ch == 'r':
        flag = DMNSN_RENDER_REFLECTION
      else:
        raise ValueError("unknown quality flag '%c'" % ch)

      if q & flag:
        raise ValueError("flag '%c' specified twice" % ch)
      else:
        q |= flag

  if inverse:
    q = ~q

  return q