Skip to content

Instantly share code, notes, and snippets.

proc split[T](self: BSPTree[T], target: Leaf, parent: ParentNode[T]): Leaf[T] =
var mp: int
parent.backward = target
if (parent of VSplit[T]):
var casted = (VSplit[T])parent
mp = midpoint(casted, target)
casted.adjustSource(target, mp)
casted.forward = casted.newSibling(target, mp)
template `isA` (a, b: untyped): untyped =
# this definition exists in the System module
compiles((b)a)
# test.nim(19, 16) Error: type mismatch: got (typedesc[Leaf[system.string]])
# but expected one of:
# template isA(a, b: untyped): untyped
type
NodeKind = enum
HSplit, VSplit, Leaf
BSPNode[T] = ref object
parent: BSPNode[T]
case kind: NodeKind
of HSplit:
hPos: float
topNode: BSPNode[T]
bottomNode: BSPNode[T]
proc hsplit[T](self: BSPNode[T], pos: float) =
if self.kind != Leaf:
return
var
width = (self.right - self.left)
midpoint = self.left + (width * pos)
# create split parent
type
NodeKind = enum
HSplit, VSplit, Leaf
BSPNode[T] = ref object
parent: BSPNode
case kind: NodeKind
of HSplit:
hPos: float
left: BSPNode
proc get[T](self: Inlet): T =
case Inlet.kind:
of Bool:
return self.boolParam.value
of Int:
return self.intParam.value
of Float:
return self.floatParam.value
of String:
return self.stringParam.value
proc send[T](self: Inlet, value: T): bool =
result = false
when T is bool:
if self.kind != Bool:
raise newException(PortKindError, "Cannot send $1 to $2" % [$(value), self.name])
if self.boolParam.set(value):
return self.boolParam.handler(value)
when T is int:
if self.kind != Int:
raise newException(PortKindError, "Cannot send $1 to $2" % [$(value), self.name])
import unittest
import strutils
include main
suite "port tests":
setup:
var
m = newModule()
m2 = newModule()
type
ValidType = concept c
c is bool or c is int
NumericType = concept c
c is ValidType
c is int
type
Parameter[T:ValidType] = object
import unittest
include main
suite "inlet tests":
setup:
var
m = newModule()
m2 = newModule()