add MSVC compiler support, make it default for Windows
new header file simd.h for SSE abstraction and helpers
  add mselect pseudo instruction for common or(and(...), andnot(...))
  replace many SSE intrinsics with new names
new MemoryPool class (mempool.h) for faster KdNode allocation
remove setMaxDepth() from Octree and KdTree, make max_depth const,
  it should be defined in constructor and never changed, change after
  building tree would cause error in traversal
modify DefaultSampler to generate nice 2x2 packets of samples for packet tracing
optimize Box and BBox::intersect_packet
add precomputed invdir attribute to RayPacket
scons build system:
  check for pthread library on Windows
  check for SDL
  generate include/config.h with variables detected by scons configuration
  move auxiliary files to build/
  add sanity checks
add writable operator[] to Vector
Help("""
Targets:
    all              - build everything,
    libs             - build all libraries
    demos            - build all demos
    models           - download/prepare all models
    docs             - compile doxygen documentation
    libs = (static-lib, python-module)
    static-lib       - ray tracer library to link with
    python-module    - ray tracer module for Python
    demos = (python-demos, cc-demos)
    python-demos     - Python demos, this depends on python-module
    cc-demos         - C++ demos
    models = (local-models, download-models)
    local-models     - prepare local models
    download-models  - download models which are not locally available
    no-docs = (libs, demos, models)
                     - everything but docs
    no-download = (libs, demos, local-models)
                     - everything but docs and downloadable models
Default target is no-download.
Options:
""")
import os, sys
EnsurePythonVersion(2, 3)
EnsureSConsVersion(0, 97)
Decider('MD5-timestamp')
SConsignFile('build/.sconsign.dblite')
if sys.platform == 'win32':
	tools = ['mingw']
else:
	tools = ['default']
env = Environment(tools = tools)
opt = Options(['build/.optioncache'])
opt.AddOptions(
	BoolOption('simd', 'allow SSE intrinsics', True),
	('precision', 'floating point number precision (single/double)', "single"),
	('flags', 'add additional compiler flags', ""),
	BoolOption('force_flags', "use only flags specified by 'flags' option (do not autodetect arch/sse flags)", False),
	BoolOption('profile', "enable gcc's profiling support (-pg)", False),
)
if env['PLATFORM'] == 'win32':
	opt.AddOptions(
		BoolOption('msvc', 'use Microsoft Visual C++ Compiler, if available', True),
		('pythonpath', 'path to Python installation',
			'C:\\Python%c%c' % (sys.version[0], sys.version[2])),
	)
else:
	opt.AddOptions(
		BoolOption('intelc', 'use Intel C++ Compiler, if available', True),
	)
opt.Update(env)
opt.Save('build/.optioncache', env)
Help(opt.GenerateHelpText(env))
### configure
platform = 'unknown'
def CheckPlatform(context):
	global platform
	context.Message('Platform is... ')
	if sys.platform[:5] == 'linux':
		platform = 'linux'
	elif env['PLATFORM'] == 'posix':
		platform = 'posix'
	elif env['PLATFORM'] == 'win32':
		platform = 'win32'
	context.Result(platform)
	return True
def CheckIntelC(context):
	global intelc, intelcversion
	context.Message('Checking for IntelC compiler... ')
	intelc = Tool("intelc").exists(env) == True
	if intelc:
		testenv = Environment()
		Tool("intelc").generate(testenv)
		intelcversion = str(testenv['INTEL_C_COMPILER_VERSION']/10.)
		context.Result(intelcversion)
	else:
		intelcversion = ''
		context.Result(intelc)
	return intelc
def CheckGCC(context):
	global gcc, gccversion
	context.Message('Checking for GCC compiler... ')
	gcc = "g++" in env['TOOLS']
	if gcc:
		gccversion = env['CCVERSION']
		context.Result(gccversion)
	else:
		gccversion = ''
		context.Result(False)
	return gcc
def CheckMSVC(context):
	global msvc, msvcversion
	context.Message('Checking for MSVC compiler... ')
	testenv = Environment()
	msvc = "msvc" in testenv['TOOLS']
	if msvc:
		msvcversion = testenv['MSVS_VERSION']
		context.Result(msvcversion)
	else:
		msvcversion = ''
		context.Result(False)
	return msvc
def CheckCPUFlags(context):
	global cpu, cpuflags_gcc, cpuflags_intelc
	context.Message('Checking CPU arch and flags... ')
	env.Execute('@$CC tools/cpuflags.c -o tools/cpuflags')
	(cpu, cpuflags_gcc, cpuflags_intelc) = os.popen('tools'+os.sep+'cpuflags %s %s'
		% (''.join(gccversion.rsplit('.',1)), intelcversion) ).read().split('\n')[:3]
	context.Result(cpu)
	return True
conf_dir = "#/build/.sconf_temp"
log_file="#/build/config.log"
config_h="#/include/config.h"
conf = Configure(env, conf_dir=conf_dir, log_file=log_file, config_h=config_h,
	custom_tests = {
		'CheckPlatform' : CheckPlatform, 'CheckCPUFlags' : CheckCPUFlags,
		'CheckIntelC' : CheckIntelC, 'CheckGCC' : CheckGCC, 'CheckMSVC' : CheckMSVC})
conf.CheckPlatform()
conf.CheckGCC()
if platform == 'win32':
	conf.CheckMSVC()
	intelc = False
else:
	conf.CheckIntelC()
	msvc=False
if intelc or gcc:
	conf.CheckCPUFlags()
if intelc and (not gcc or conf.env['intelc']):
	Tool('intelc').generate(conf.env)
	cc = 'intelc'
elif msvc and (not gcc or conf.env['msvc']):
	Tool('default').generate(conf.env)
	conf.Define("MSVC")
	cc = 'msvc'
elif gcc:
	cc = 'gcc'
else:
	cc = 'none'
if platform == 'win32' and cc == 'gcc':
	conf.env.Append(LIBPATH=["C:/mingw/lib", "C:/msys/mingw/lib"])
	conf.env.Append(CPPPATH=["C:/mingw/include", "C:/msys/mingw/include"])
add_flags = ''
if cc == 'gcc':
	add_flags += cpuflags_gcc + ' -ffast-math '
if cc == 'intelc':
	add_flags += cpuflags_intelc + ' '
if cc == 'msvc':
	add_flags += '/fp:fast '
	if conf.env['simd']:
		add_flags += '/arch:SSE '
if conf.env['force_flags']:
	add_flags = conf.env['flags'] + ' '
else:
	add_flags += conf.env['flags'] + ' '
if conf.env['precision'] == 'double':
	conf.Define("PYRIT_DOUBLE")
elif cc == 'gcc':
	add_flags += '-fsingle-precision-constant '
if not conf.env['simd'] or conf.env['precision'] == 'double':
	conf.Define("NO_SIMD")
if cc == 'intelc':
	conf.env.Append(CCFLAGS="-O3 -w1 " + add_flags)
elif cc == 'gcc':
	conf.env.Append(CCFLAGS="-O3 -Wall -pipe " + add_flags)
elif cc == 'msvc':
	conf.env.Append(CCFLAGS="/Ox /Ob2 /GS- /Gy /GF /GR- /Zp16 /MD /EHsc /vmb " + add_flags)
else:
	print "No supported compiler found."
	Exit(1)
print "Using compiler: " + cc
print "Additional flags: " + add_flags
pthread = True
if conf.env['PLATFORM'] == 'win32':
	if cc == 'msvc':
		if not conf.CheckLib('pthreadVC2'):
			pthread = False
		conf.env.Append(LIBS=["pthreadVC2"])
	elif cc == 'gcc':
		if not conf.CheckLib('pthreadGC2'):
			pthread = False
		conf.env.Append(LIBS=["pthreadGC2"])
else:
	conf.env.Append(CCFLAGS="-pthread ")
if not pthread:
	print 'Error: Cannot build without pthread.'
	Exit(1)
if conf.CheckLibWithHeader('png', 'png.h', 'C'):
	conf.Define('HAVE_PNG')
	conf.env.Append(LIBS=['png'])
elif conf.CheckLib('libpng13'):
	conf.Define('HAVE_PNG')
	conf.env.Append(LIBS=['libpng13'])
if conf.env['profile'] and cc == 'gcc':
	conf.env.Append(CCFLAGS="-pg", LINKFLAGS="-pg")
env = conf.Finish()
# configure SDL
sdlenv = env.Clone()
if cc != 'msvc':
	try:
		sdlenv.ParseConfig('sh sdl-config --cflags')
		sdlenv.ParseConfig('sh sdl-config --libs')
	except:
		pass
else:
	sdlenv.Append(LIBS=['SDL', 'SDLmain'])
conf = Configure(sdlenv, conf_dir=conf_dir, log_file=log_file, config_h=config_h)
have_sdl = False
if conf.CheckLib('SDL'):
	have_sdl = True
else:
	print "SDL not found, some demos will not built."
sdlenv = conf.Finish()
if cc == 'msvc':
	sdlenv.Append(LINKFLAGS="/SUBSYSTEM:WINDOWS")
### build targets
Export('env sdlenv cc')
lib = SConscript('src/SConscript', build_dir='build/lib', duplicate=0,
	exports={'buildmodule':False})
pymodule = SConscript('src/SConscript', build_dir='build/pymodule', duplicate=0,
	exports={'buildmodule':True})
if have_sdl:
	SConscript('ccdemos/SConscript', build_dir='build/ccdemos', duplicate=0,
		exports='lib')
SConscript('demos/SConscript', exports='pymodule')
SConscript('tests/SConscript', build_dir='build/tests', duplicate=0, exports='lib')
SConscript('models/SConscript')
env.Alias('demos', ['cc-demos', 'python-demos'])
env.Alias('libs', ['static-lib', 'python-module'])
env.Alias('docs', Command('docs/html', [], 'doxygen'))
env.Clean('docs', ['docs/html'])
env.Alias('no-docs', ['libs', 'demos', 'models'])
env.Alias('no-download', ['libs', 'demos', 'local-models'])
env.Alias('all', ['no-docs', 'docs'])
env.Alias('pyrit', 'no-download')
Default('pyrit')