void ccContourExtractorDlg::zoomOn(const ccBBox& box)
{
	if (!m_glWindow)
		return;

	float pixSize = std::max(box.getDiagVec().x / std::max(20,m_glWindow->width()-20), box.getDiagVec().y / std::max(20,m_glWindow->height()-20));
	m_glWindow->setPixelSize(pixSize);
	m_glWindow->setCameraPos(CCVector3d::fromArray(box.getCenter().u));
}
void cc2Point5DimEditor::update2DDisplayZoom(ccBBox& box)
{
	if (!m_window || !m_grid.isValid())
		return;

	//equivalent to 'ccGLWindow::updateConstellationCenterAndZoom' but we take aspect ratio into account

	//we compute the pixel size (in world coordinates)
	{
		ccViewportParameters params = m_window->getViewportParameters();

		double realGridWidth  = m_grid.width  * m_grid.gridStep;
		double realGridHeight = m_grid.height * m_grid.gridStep;

		static const int screnMargin = 20;
		int screenWidth  = std::max(1,m_window->width()  - 2*screnMargin);
		int screenHeight = std::max(1,m_window->height() - 2*screnMargin);

		int pointSize = 1;
		if (	static_cast<int>(m_grid.width)  < screenWidth
			&&	static_cast<int>(m_grid.height) < screenHeight)
		{
			int vPointSize = static_cast<int>(ceil(static_cast<float>(screenWidth) /m_grid.width));
			int hPointSize = static_cast<int>(ceil(static_cast<float>(screenHeight)/m_grid.height));
			pointSize = std::min(vPointSize, hPointSize);

			//if the grid is too small (i.e. necessary point size > 10)
			if (pointSize > 10)
			{
				pointSize = 10;
				screenWidth  = m_grid.width  * pointSize;
				screenHeight = m_grid.height * pointSize;
			}
		}

		params.pixelSize = static_cast<float>( std::max( realGridWidth/screenWidth, realGridHeight/screenHeight ) );
		params.zoom = 1.0f;

		m_window->setViewportParameters(params);
		m_window->setPointSize(pointSize);
	}
	
	//we set the pivot point on the box center
	CCVector3 P = box.getCenter();
	m_window->setPivotPoint(CCVector3d::fromArray(P.u));
	m_window->setCameraPos(CCVector3d::fromArray(P.u));

	m_window->invalidateViewport();
	m_window->invalidateVisualization();
	m_window->redraw();
}
//Helper
void MakeSquare(ccBBox& box, int pivotType, int defaultDim = -1)
{
	assert(defaultDim<3);
	assert(pivotType>=0 && pivotType<3);

	CCVector3 W = box.getDiagVec();
	if (W.x != W.y || W.x != W.z)
	{
		if (defaultDim < 0)
		{
			//we take the largest one!
			defaultDim = 0;
			if (W.u[1] > W.u[defaultDim])
				defaultDim = 1;
			if (W.u[2] > W.u[defaultDim])
				defaultDim = 2;
		}

		CCVector3 newW(W.u[defaultDim], W.u[defaultDim], W.u[defaultDim]);
		switch(pivotType)
		{
		case 0: //min corner
			{
				CCVector3 A = box.minCorner();
				box = ccBBox(A, A + newW);
			}
			break;
		case 1: //center
			{
				CCVector3 C = box.getCenter();
				box = ccBBox(C - newW / 2.0, C + newW / 2.0);
			}
			break;
		case 2: //max corner
			{
				CCVector3 B = box.maxCorner();
				box = ccBBox(B-newW,B);
			}
			break;
		}
	}
}