///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2004 Intel Corporation 
// All rights reserved. 
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are met: 
//
// * Redistributions of source code must retain the above copyright notice, 
// this list of conditions and the following disclaimer. 
// * Redistributions in binary form must reproduce the above copyright notice, 
// this list of conditions and the following disclaimer in the documentation 
// and/or other materials provided with the distribution. 
// * Neither name of Intel Corporation nor the names of its contributors 
// may be used to endorse or promote products derived from this software 
// without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////

// testDoc.cpp : implementation of the CTestDoc class
//

#include "stdafx.h"
#include "ObjectViewer.h"

#include "ObjectViewerDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestDoc

IMPLEMENT_DYNCREATE(CTestDoc, CDocument)

BEGIN_MESSAGE_MAP(CTestDoc, CDocument)
	//{{AFX_MSG_MAP(CTestDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDoc construction/destruction

CTestDoc::CTestDoc()
{
	m_pData = 0;
	m_pData2 = 0;
}

CTestDoc::~CTestDoc()
{
	if (m_pData) { delete m_pData; m_pData = 0; }
	if (m_pData) { delete m_pData; m_pData = 0; }
}

BOOL CTestDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
//	if (!m_pData) m_pData = new Data;

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CTestDoc serialization

void CTestDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CTestDoc diagnostics

#ifdef _DEBUG
void CTestDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CTestDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestDoc commands

BOOL CTestDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	m_fileName.Format(lpszPathName);

	//Read data from the file and allocate memory for data1  
	if (m_pData)  { delete m_pData; m_pData = 0; }
	m_pData = new Object3DS;
	FILE* pFile = fopen(lpszPathName, "rb");
	m_pData->Load(pFile);
	m_pData->Normalize();
	m_pData->CalcPlanes();
	fclose(pFile);
	
	return TRUE;
}

void CTestDoc::DuplicateObject()
{
	if (!m_pData2)
	{
		m_pData2 = new Object3DS;
		FILE* pFile = fopen(m_fileName, "rb");
		m_pData2->Load(pFile);
		m_pData2->Normalize();
		m_pData2->CalcPlanes();
		fclose(pFile);
	}
}

void CTestDoc::OnCloseDocument() 
{
	// TODO: Add your specialized code here and/or call the base class
	if (m_pData) { delete m_pData; m_pData = 0; }
	
	CDocument::OnCloseDocument();
}

void CTestDoc::Reinit()
{
//	m_pData->Normalize();
	m_pData->CalcPlanes();
}

void CTestDoc::Reload()
{
	if (m_pData)  { delete m_pData; m_pData = 0; }
	m_pData = new Object3DS;
	FILE* pFile = fopen(m_fileName, "rb");
	m_pData->Load(pFile);
	m_pData->Normalize();
	m_pData->CalcPlanes();
}
