Beispiel #1
0
func TestBoundingBoxSplit(t *testing.T) {
	box := &BoundingBox{
		MinVolume: [3]float64{-3.18, -3.96, -1.77},
		MaxVolume: [3]float64{4.58, 1.74, 4.56},
	}

	ray := &ray.Ray{
		Start:     *maths.NewVec3(4.49, -7.3, 5.53),
		Direction: *maths.NewVec3(-0.60, 0.5, -0.62),
	}
	ray.Init()

	if !box.Intersect(ray) {
		t.Error("ray should intersect big box")
	}

	left, right := box.Split(2, 1.39)

	assertEqualVectors(t, maths.NewVec3(-3.18, -3.96, -1.77), maths.NewVec3Array(left.MinVolume))
	assertEqualVectors(t, maths.NewVec3(-3.18, -3.96, 1.39), maths.NewVec3Array(right.MinVolume))
	assertEqualVectors(t, maths.NewVec3(4.58, 1.74, 1.39), maths.NewVec3Array(left.MaxVolume))
	assertEqualVectors(t, maths.NewVec3(4.58, 1.74, 4.56), maths.NewVec3Array(right.MaxVolume))

	if box.IntersectWall(2, 1.39, ray) {
		t.Error("ray shouldn't intersect the wall")
	}

	if right.Intersect(ray) {
		t.Error("ray shouldn't intersect right")
	}

	if !left.Intersect(ray) {
		t.Error("ray should intersect left")
	}
}
Beispiel #2
0
func TestGetBoundingBox(t *testing.T) {
	meshData := []byte(`{
		"vertices": [
			{
				"normal": [0, 0, 1],
				"coordinates": [-1, -2, 0]
			},
			{
				"normal": [0, 0, 1],
				"coordinates": [1, 5, -8]
			},
			{
				"normal": [0, 0, 1],
				"coordinates": [1, 2, 0]
			},
			{
				"normal": [0, 0, 1],
				"coordinates": [1, 1, 0]
			}
		],
		"faces": [
			{
				"vertices": [0, 1, 2],
				"material": 42
			},
			{
				"vertices": [1, 2, 3],
				"material": 42
			}
		]
	}`)
	mesh := &Mesh{}
	err := json.Unmarshal(meshData, &mesh)
	if err != nil {
		t.Fatalf("Error reading json: %s\n", err)
		return
	}

	mesh.Init()

	bbox := mesh.GetBoundingBox()

	assertEqualVectors(t, maths.NewVec3(1, 5, 0), maths.NewVec3Array(bbox.MaxVolume))
	assertEqualVectors(t, maths.NewVec3(-1, -2, -8), maths.NewVec3Array(bbox.MinVolume))
}
Beispiel #3
0
// String returns the string representation of the boundingBox in the form of bbox[min: _, max: _]
func (b *BoundingBox) String() string {
	return fmt.Sprintf("bbox[min: %s, max: %s]", maths.NewVec3Array(b.MinVolume), maths.NewVec3Array(b.MaxVolume))
}