Sample Code: Set exact world fit with no gaps

Version: HPS 2020
Language: C#
Description:

This sample code behaves similarly to View::FitWorld(), but it does not include gaps.

void SetExactWorldFit(Canvas _canvas)
{
	HPS::SegmentKey viewKey = _canvas.GetFrontView().GetSegmentKey();
	HPS::Rectangle screenExtent;
	
	// SprocketPath is  a key path from the model segment up to the canvas window key, including all segments and include keys along the way
	HPS::SprocketPath sprkPath = SprocketPath(_canvas);
	
	// Compute a 2D extent box around the model in Screen Space 
	sprkPath.GetKeyPath().ComputeExtent(screenExtent);
	
	//  Compute the center point of box in Screen Space
	HPS::Point lefttop = HPS::Point(screenExtent.left, screenExtent.top, 0);
	HPS::Point bottomright = HPS::Point(screenExtent.right, screenExtent.bottom, 0);
	HPS::Point center = HPS::Point((lefttop.x + bottomright.x) / 2.0f,
		(lefttop.y + bottomright.y) / 2.0f,
		0);
	
	sprkPath.GetKeyPath().ConvertCoordinate(HPS::Coordinate::Space::Window, center, HPS::Coordinate::Space::Camera, center);
	center.z = 0;
	
	// Get information on current camera
	CameraKit out_kit;
	sprkPath.GetKeyPath().ShowNetCamera(out_kit);
	HPS::Point pos;
	out_kit.ShowPosition(pos);
	
	sprkPath.GetKeyPath().ConvertCoordinate(HPS::Coordinate::Space::World, pos, HPS::Coordinate::Space::Camera, pos);
	HPS::Point delta = Point(pos.x - center.x, pos.y - center.y, pos.z - center.z);
	
	//  move camera
	viewKey.GetCameraControl().Dolly(-delta.x, -delta.y, 0);
	
	//  Calculate and set camera field for an exact fit
	sprkPath.GetKeyPath().ConvertCoordinate(HPS::Coordinate::Space::Window, lefttop, HPS::Coordinate::Space::Camera, lefttop);
	sprkPath.GetKeyPath().ConvertCoordinate(HPS::Coordinate::Space::Window, bottomright, HPS::Coordinate::Space::Camera, bottomright);
	
	HPS::Point vector = Point(lefttop.x - bottomright.x, lefttop.y - bottomright.y, lefttop.z - bottomright.z);
	vector.x = abs(vector.x);
	vector.y = abs(vector.y);
	
	viewKey.GetCameraControl().SetField(vector.x, vector.y);
	
	_canvas.UpdateWithNotifier().Wait();
}