Commit ee82a0ba authored by Jan's avatar Jan
Browse files

Removed Poisson

parent 53ba31f3
include (plugin)
openflipper_plugin (DIRS PoissonReconstruction
INSTALLDATA Icons
TYPES TRIANGLEMESH POLYMESH
OPT_TYPES SPLATCLOUD )
/*
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
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 the name of the Johns Hopkins University 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 THE COPYRIGHT OWNER 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.
*/
#ifndef ALLOCATOR_INCLUDED
#define ALLOCATOR_INCLUDED
#include <vector>
class AllocatorState{
public:
int index,remains;
};
/** This templated class assists in memory allocation and is well suited for instances
* when it is known that the sequence of memory allocations is performed in a stack-based
* manner, so that memory allocated last is released first. It also preallocates memory
* in chunks so that multiple requests for small chunks of memory do not require separate
* system calls to the memory manager.
* The allocator is templated off of the class of objects that we would like it to allocate,
* ensuring that appropriate constructors and destructors are called as necessary.
*/
template<class T>
class AllocatorT{
size_t blockSize;
size_t index,remains;
std::vector<T*> memory;
public:
AllocatorT(void){
blockSize=index=remains=0;
}
~AllocatorT(void){
reset();
}
/** This method is the allocators destructor. It frees up any of the memory that
* it has allocated. */
void reset(void){
for(size_t i=0;i<memory.size();i++){delete[] memory[i];}
memory.clear();
blockSize=index=remains=0;
}
/** This method returns the memory state of the allocator. */
AllocatorState getState(void) const{
AllocatorState s;
s.index=index;
s.remains=remains;
return s;
}
/** This method rolls back the allocator so that it makes all of the memory previously
* allocated available for re-allocation. Note that it does it not call the constructor
* again, so after this method has been called, assumptions about the state of the values
* in memory are no longer valid. */
void rollBack(void){
if( !memory.empty() ){
for(size_t i=0;i<memory.size();i++){
for(int j=0;j<blockSize;j++){
memory[i][j].~T();
new(&memory[i][j]) T();
}
}
index=0;
remains=blockSize;
}
}
/** This method rolls back the allocator to the previous memory state and makes all of the memory previously
* allocated available for re-allocation. Note that it does it not call the constructor
* again, so after this method has been called, assumptions about the state of the values
* in memory are no longer valid. */
void rollBack(const AllocatorState& state){
if(state.index<index || (state.index==index && state.remains<remains)){
if(state.index<index){
for(int j=state.remains;j<blockSize;j++){
memory[state.index][j].~T();
new(&memory[state.index][j]) T();
}
for(int i=state.index+1;i<index-1;i++){
for(int j=0;j<blockSize;j++){
memory[i][j].~T();
new(&memory[i][j]) T();
}
}
for(int j=0;j<remains;j++){
memory[index][j].~T();
new(&memory[index][j]) T();
}
index=state.index;
remains=state.remains;
}
else{
for(int j=0;j<state.remains;j<remains){
memory[index][j].~T();
new(&memory[index][j]) T();
}
remains=state.remains;
}
}
}
/** This method initializes the constructor and the blockSize variable specifies the
* the number of objects that should be pre-allocated at a time. */
void set( int blockSize){
reset();
this->blockSize=blockSize;
index=-1;
remains=0;
}
/** This method returns a pointer to an array of elements objects. If there is left over pre-allocated
* memory, this method simply returns a pointer to the next free piece of memory, otherwise it pre-allocates
* more memory. Note that if the number of objects requested is larger than the value blockSize with which
* the allocator was initialized, the request for memory will fail.
*/
T* newElements( size_t elements=1){
T* mem;
if(!elements){return NULL;}
if(elements>blockSize){
std::cerr << "Allocator Error, elements bigger than block-size: " << elements << ">" << "blockSize" << std::endl;
return NULL;
}
if(remains<elements){
if(index==memory.size()-1){
mem=new T[blockSize];
if(!mem){fprintf(stderr,"Failed to allocate memory\n");exit(0);}
memory.push_back(mem);
}
index++;
remains=blockSize;
}
mem=&(memory[index][blockSize-remains]);
remains-=elements;
return mem;
}
};
#endif // ALLOCATOR_INCLUDE
/*
Copyright (c) 2011, Michael Kazhdan and Ming Chuang
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 the name of the Johns Hopkins University 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 THE COPYRIGHT OWNER 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.
*/
#ifndef ARRAY_INCLUDED
#define ARRAY_INCLUDED
#include <vector>
#define ARRAY_DEBUG 0
#ifdef _WIN64
#define ASSERT( x ) { if( !( x ) ) __debugbreak(); }
#else // !_WIN64
#ifdef _WIN32
#define ASSERT( x ) { if( !( x ) ) _asm{ int 0x03 } }
#else // !_WIN32
#define ASSERT( x ) { if( !( x ) ) exit(0); }
#endif // _WIN32
#endif // _WIN64
// Code from http://stackoverflow.com
void* aligned_malloc( size_t size , size_t align )
{
// Align enough for the data, the alignment padding, and room to store a pointer to the actual start of the memory
void* mem = malloc( size + align + sizeof( void* ) );
// The position at which we could potentially start addressing
char* amem = ( (char*)mem ) + sizeof( void* );
// Add align-1 to the start of the address and then zero out at most of the first align-1 bits.
amem = ( char* )( ( (size_t)( ( (char*)amem ) + (align-1) ) ) & ~( align-1 ) );
// Pre-write the actual address
( ( void** ) amem )[-1] = mem;
return amem;
}
void aligned_free( void* mem ) { free( ( ( void** )mem )[-1] ); }
#if ARRAY_DEBUG
#pragma message ( "[WARNING] Array debugging is enabled" )
#include "Array.inl"
#define Pointer( ... ) Array< __VA_ARGS__ >
#define ConstPointer( ... ) ConstArray< __VA_ARGS__ >
template< class C > void FreePointer( Array< C >& a ){ a.Free( ); }
template< class C > void AlignedFreePointer( Array< C >& a ){ a.Free( ); }
template< class C > void VFreePointer( Array< C >& a ){ a.Free( ); }
template< class C > void DeletePointer( Array< C >& a ){ a.Delete( ); }
template< class C > Array< C > NewPointer( size_t size , const char* name=NULL ){ return Array< C >::New ( size , name ); }
template< class C > Array< C > AllocPointer( size_t size , const char* name=NULL ){ return Array< C >::Alloc ( size , false , name ); }
template< class C > Array< C > AlignedAllocPointer( size_t size , size_t alignment , const char* name=NULL ){ return Array< C >::AlignedAlloc( size , alignment , false , name ); }
template< class C > Array< C > ReAllocPointer( Array< C >& a , size_t size , const char* name=NULL ){ return Array< C >::ReAlloc ( a , size , false , name ); }
template< class C > Array< C > NullPointer( void ){ return Array< C >( ); }
template< class C > C* PointerAddress( Array< C >& a ) { return a.pointer(); }
template< class C > const C* PointerAddress( ConstArray< C >& a ) { return a.pointer(); }
template< class C > Array< C > GetPointer( C& c ) { return Array< C >::FromPointer( &c , 1 ); }
template< class C > ConstArray< C > GetPointer( const C& c ) { return ConstArray< C >::FromPointer( &c , 1 ); }
template< class C > Array< C > GetPointer( std::vector< C >& v ){ return Array< C >::FromPointer( &v[0] , v.size() ); }
template< class C > ConstArray< C > GetPointer( const std::vector< C >& v ){ return ConstArray< C >::FromPointer( &v[0] , v.size() ); }
#else // !ARRAY_DEBUG
#define Pointer( ... ) __VA_ARGS__*
#define ConstPointer( ... ) const __VA_ARGS__*
#define FreePointer( ... ) { if( __VA_ARGS__ ) free( __VA_ARGS__ ) , __VA_ARGS__ = NULL; }
#define AlignedFreePointer( ... ) { if( __VA_ARGS__ ) aligned_free( __VA_ARGS__ ) , __VA_ARGS__ = NULL; }
#define DeletePointer( ... ) { if( __VA_ARGS__ ) delete[] __VA_ARGS__ , __VA_ARGS__ = NULL; }
template< class C > C* NewPointer( size_t size , const char* name=NULL ){ return new C[size]; }
template< class C > C* AllocPointer( size_t size , const char* name=NULL ){ return static_cast<C*> (malloc( sizeof(C) * size )); }
template< class C > C* AlignedAllocPointer( size_t size , size_t alignment , const char* name=NULL ){ return static_cast<C*>(aligned_malloc( sizeof(C) * size , alignment )); }
template< class C > C* ReAllocPointer( C* c , size_t size , const char* name=NULL ){ return static_cast<C*> (realloc( c , sizeof(C) * size )); }
template< class C > C* NullPointer( void ){ return NULL; }
template< class C > C* PointerAddress( C* c ){ return c; }
template< class C > const C* PointerAddress( const C* c ){ return c; }
template< class C > C* GetPointer( C& c ){ return &c; }
template< class C > const C* GetPointer( const C& c ){ return &c; }
template< class C > C* GetPointer( std::vector< C >& v ){ return &v[0]; }
template< class C > const C* GetPointer( const std::vector< C >& v ){ return &v[0]; }
#endif // ARRAY_DEBUG
#endif // ARRAY_INCLUDED
/*
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
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 the name of the Johns Hopkins University 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 THE COPYRIGHT OWNER 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.
*/
#ifndef BSPLINE_DATA_INCLUDED
#define BSPLINE_DATA_INCLUDED
#include "PPolynomial.h"
#include "Array.h"
template< int Degree >
struct BSplineElementCoefficients
{
// cppcheck-suppress negativeIndex
int coeffs[Degree+1];
BSplineElementCoefficients( void ) { memset( coeffs , 0 , sizeof( int ) * ( Degree+1 ) ); }
int& operator[]( unsigned int idx ){ if (idx <= Degree) return coeffs[idx]; return coeffs[0];}
const int& operator[]( unsigned int idx ) const {if (idx <= Degree) return coeffs[idx]; return coeffs[0];}
};
template< int Degree >
struct BSplineElements : public std::vector< BSplineElementCoefficients< Degree > >
{
static const int _off = (Degree+1)/2;
void _addLeft ( int offset , int boundary );
void _addRight( int offset , int boundary );
public:
enum
{
NONE = 0,
DIRICHLET = -1,
NEUMANN = 1
};
// Coefficients are ordered as "/" "-" "\"
int denominator;
BSplineElements( void ) { denominator = 1; }
BSplineElements( int res , int offset , int boundary=NONE , int inset=0 );
void upSample( BSplineElements& high ) const;
void differentiate( BSplineElements< Degree-1 >& d ) const;
void print( FILE* fp=stdout ) const
{
for( int i=0 ; i< this->size() ; i++ )
{
printf( "%d]" , i );
for( int j=0 ; j<=Degree ; j++ ) printf( " %d" , (*this)[i][j] );
printf( " (%d)\n" , denominator );
}
}
};
template< int Degree , class Real >
class BSplineData
{
bool useDotRatios;
int boundaryType;
public:
struct BSplineComponents
{
Polynomial< Degree > polys[Degree+1];
Polynomial< Degree >& operator[] ( unsigned int idx ) { return polys[idx]; }
const Polynomial< Degree >& operator[] ( unsigned int idx ) const { return polys[idx]; }
void printnl( void ) const { for( int d=0 ; d<=Degree ; d++ ) polys[d].printnl(); }
BSplineComponents scale( double s ) const { BSplineComponents b ; for( unsigned int d=0 ; d<=Degree ; d++ ) b[d] = polys[d].scale(s) ; return b; }
BSplineComponents shift( double s ) const { BSplineComponents b ; for( unsigned int d=0 ; d<=Degree ; d++ ) b[d] = polys[d].shift(s) ; return b; }
};
const static int VV_DOT_FLAG = 1;
const static int DV_DOT_FLAG = 2;
const static int DD_DOT_FLAG = 4;
const static int VALUE_FLAG = 1;
const static int D_VALUE_FLAG = 2;
int depth , functionCount , sampleCount;
Pointer( Real ) vvDotTable;
Pointer( Real ) dvDotTable;
Pointer( Real ) ddDotTable;
Pointer( Real ) valueTables;
Pointer( Real ) dValueTables;
PPolynomial< Degree > baseFunction , leftBaseFunction , rightBaseFunction , leftRightBaseFunction;
PPolynomial< Degree-1 > dBaseFunction , dLeftBaseFunction , dRightBaseFunction , dLeftRightBaseFunction;
BSplineComponents baseBSpline , leftBSpline , rightBSpline , leftRightBSpline;
Pointer( PPolynomial< Degree > ) baseFunctions;
Pointer( BSplineComponents ) baseBSplines;
BSplineData(void);
~BSplineData(void);
virtual void setDotTables( int flags , bool inset=false );
virtual void clearDotTables( int flags );
virtual void setValueTables( int flags , double smooth=0 );
virtual void setValueTables( int flags , double valueSmooth , double normalSmooth );
virtual void clearValueTables( void );
void setSampleSpan( int idx , int& start , int& end , double smooth=0 ) const;
/********************************************************
* Sets the translates and scales of the basis function
* up to the prescribed depth
* <maxDepth> the maximum depth
* <useDotRatios> specifies if dot-products of derivatives
* should be pre-divided by function integrals
* <reflectBoundary> spcifies if function space should be
* forced to be reflectively symmetric across the boundary
********************************************************/
void set( int maxDepth , bool useDotRatios=true , int boundaryType=BSplineElements< Degree >::NONE );
inline int Index( int i1 , int i2 ) const;
static inline int SymmetricIndex( int i1 , int i2 );
static inline int SymmetricIndex( int i1 , int i2 , int& index );
};
template< int Degree1 , int Degree2 > void SetBSplineElementIntegrals( double integrals[Degree1+1][Degree2+1] );
#include "BSplineData.inl"
#endif // BSPLINE_DATA_INCLUDED
/*
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
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 the name of the Johns Hopkins University 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 THE COPYRIGHT OWNER 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.
*/
#ifndef BINARY_NODE_INCLUDED
#define BINARY_NODE_INCLUDED
#define MSVC_2010_FIX 1
template<class Real>
class BinaryNode
{
public:
static inline int CenterCount( int depth ) { return 1<<depth; }
static inline int CornerCount( int depth ) { return (1<<depth)+1; }
static inline int CumulativeCenterCount( int maxDepth ) { return (1<<(maxDepth+1))-1; }
static inline int CumulativeCornerCount( int maxDepth ) { return (1<<(maxDepth+1))+maxDepth; }
static inline int CenterIndex( int depth , int offSet ) { return (1<<depth)+offSet-1; }
static inline int CornerIndex( int depth , int offSet ) { return (1<<depth)+offSet+depth; }
static inline int CornerIndex( int maxDepth , int depth , int offSet , int forwardCorner ){ return (offSet+forwardCorner)<<(maxDepth-depth); }
static inline Real CornerIndexPosition(int index,int maxDepth){ return Real(index)/(1<<maxDepth); }
static inline Real Width(int depth){ return Real(1.0/(1<<depth)); }
static inline void CenterAndWidth( int depth , int offset , Real& center , Real& width )
{
width=Real (1.0/(1<<depth) );
center=Real((0.5+offset)*width);
}
static inline void CenterAndWidth( int idx , Real& center , Real& width )
{
int depth , offset;
DepthAndOffset( idx , depth , offset );
CenterAndWidth( depth , offset , center , width );
}
static inline void DepthAndOffset( int idx , int& depth , int& offset )
{
int i=idx+1;
#if MSVC_2010_FIX
depth = 0;
#else // !MSVC_2010_FIX
depth = -1;
#endif // MSVC_2010_FIX
while( i )
{
i >>= 1;
depth++;
}
#if MSVC_2010_FIX
depth--;
#endif // MSVC_2010_FIX
offset = ( idx+1 ) - (1<<depth);
}
};
#endif // BINARY_NODE_INCLUDED
/*
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
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 the name of the Johns Hopkins University 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 THE COPYRIGHT OWNER 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.
*/
//////////////////////
// Polynomial Roots //
//////////////////////
#include <math.h>
#include "Factor.h"
int Factor(double a1,double a0,double roots[1][2],double EPS){
if(fabs(a1)<=EPS){return 0;}
roots[0][0]=-a0/a1;
roots[0][1]=0;
return 1;
}
int Factor(double a2,double a1,double a0,double roots[2][2],double EPS){
double d;
if(fabs(a2)<=EPS){return Factor(a1,a0,roots,EPS);}
d=a1*a1-4*a0*a2;
a1/=(2*a2);
if(d<0){
d=sqrt(-d)/(2*a2);
roots[0][0]=roots[1][0]=-a1;
roots[0][1]=-d;
roots[1][1]= d;
}
else{
d=sqrt(d)/(2*a2);
roots[0][1]=roots[1][1]=0;
roots[0][0]=-a1-d;
roots[1][0]=-a1+d;
}
return 2;
}
// Solution taken from: http://mathworld.wolfram.com/CubicFormula.html
// and http://www.csit.fsu.edu/~burkardt/f_src/subpak/subpak.f90