Esempio n. 1
0
void* ON_Workspace::GetMemory( size_t size )
{
  void* p = NULL;
  if ( size > 0 ) 
  {
    struct ON_Workspace_MBLK* pBlk = (struct ON_Workspace_MBLK*)onmalloc(sizeof(*pBlk));
    if ( pBlk ) 
    {
      pBlk->pMem = p = onmalloc(size);
      pBlk->pNext = m_pMemBlk;
      m_pMemBlk = pBlk;
    }
  }
  return p;
}
Esempio n. 2
0
bool ON_Matrix::Create( int row_count, int col_count)
{
  bool b = false;
  Destroy();
  if ( row_count > 0 && col_count > 0 ) 
  {
    m_rowmem.Reserve(row_count);
    if ( 0 != m_rowmem.Array() )
    {
      m_rowmem.SetCount(row_count);
      // In general, allocate coefficient memory in chunks 
      // of <= max_dblblk_size bytes.  The value of max_dblblk_size
      // is tuned to maximize speed on calculations involving
      // large matrices.  If all of the coefficients will fit
      // into a chunk of memory <= 1.1*max_dblblk_size, then 
      // a single chunk is allocated.
      
      // In limited testing, these two values appeared to work ok.
      // The latter was a hair faster in solving large row reduction
      // problems (for reasons I do not understand).
      //const int max_dblblk_size = 1024*1024*8;
      const int max_dblblk_size = 512*1024;

      int rows_per_block = max_dblblk_size/(col_count*sizeof(double));
      if ( rows_per_block > row_count )
        rows_per_block = row_count;
      else if ( rows_per_block < 1 )
        rows_per_block = 1;
      else if ( rows_per_block < row_count && 11*rows_per_block >= 10*row_count )
        rows_per_block = row_count;

      int j, i = row_count; 
      m = m_rowmem.Array();
      double** row = m;
      for ( i = row_count; i > 0; i -= rows_per_block )
      {
        if ( i < rows_per_block )
          rows_per_block = i;
        int dblblk_count = rows_per_block*col_count;
        struct DBLBLK* p = (struct DBLBLK*)onmalloc(sizeof(*p) + dblblk_count*sizeof(p->a[0]));
        p->a = (double*)(p+1);
        p->count = dblblk_count;
        p->next = (struct DBLBLK*)m_cmem;
        m_cmem = p;
        *row = p->a;
        j = rows_per_block-1;
        while(j--)
        {
          row[1] = row[0] + col_count;
          row++;
        }
        row++;
      }
      m_row_count = row_count;
      m_col_count = col_count;
      b = true;
    }
  }
  return b;
}
ON_3dPoint ON_BezierCage::PointAt( ON_3dPoint rst ) const
{
  ON_3dPoint pt;
  if ( m_dim <= 3 )
  {
    pt.x  = 0.0;
    pt.y  = 0.0;
    pt.z  = 0.0;
    Evaluate(rst.x,rst.y,rst.z,0,3,&pt.x);
  }
  else
  {
    double stack_buffer[16];
    double* v;
    size_t sizeof_buffer = m_dim*sizeof(*v);
    v = (sizeof_buffer <= sizeof(stack_buffer)) ? stack_buffer : (double*)onmalloc(sizeof_buffer);
    v[0] = 0.0;
    v[1] = 0.0;
    v[2] = 0.0;
    Evaluate(rst.x,rst.y,rst.z,0,m_dim,v);
    pt.x = v[0];
    pt.y = v[1];
    pt.z = v[2];
    if ( v != stack_buffer )
      onfree(v);
  }
  return pt;
}
Esempio n. 4
0
//Do not change SpanCount() without making sure it gives the correct result for use in GetSpanVector()
int ON_CurveProxy::SpanCount() const
{
  if (!m_real_curve) return 0;
  int rsc = m_real_curve->SpanCount();
  ON_Interval domain = m_real_curve->Domain();
  if (m_real_curve_domain == domain) 
    return rsc;
  double* rsv = (double*)onmalloc((rsc+1)*sizeof(double));
  if (!rsv) return 0;
  if (!m_real_curve->GetSpanVector(rsv)){
    onfree((void*)rsv);
    return 0;
  }

  int i=0;
  int sc = 0;
  
  while (i <= rsc && rsv[i] <= m_real_curve_domain[0]) i++;
  while (i <= rsc && rsv[i] < m_real_curve_domain[1]){
    sc++;
    i++;
  }
  sc++;
  onfree((void*)rsv);

  return sc;
}
Esempio n. 5
0
struct ON_MeshNgon* ON_MeshNgonList::AddNgon(int N)
{
  if ( N < 3 || N > 100000 )
    return 0;

  if ( m_ngons_count >= m_ngons_capacity )
  {
    int capacity = 2*m_ngons_count;
    if (capacity < m_ngons_count+16)
      capacity = m_ngons_count+16;
    if ( !ReserveNgonCapacity(capacity) )
      return 0;
  }
  ON_MeshNgon& ngon = m_ngons[m_ngons_count++];

  ngon.N = N;
  struct ON_NGON_MEMBLK* blk = (struct ON_NGON_MEMBLK*)onmalloc(sizeof(*blk) + (2*N)*sizeof(int));
  if ( 0 == blk )
    return 0;
  ngon.vi = (int*)(blk + 1);
  ngon.fi = ngon.vi + N;
  memset(ngon.vi,0xFF,(2*N)*sizeof(int)); // set all indicies to -1
  blk->next = m_memblk_list;
  m_memblk_list = blk;
  return &ngon;
}
Esempio n. 6
0
void* ON_Object::operator new[] (size_t sz)
{
  // ON_Object array new

  // The else "sz?sz:1" is there because section 3.7.3.1, 
  // paragraph 2 of the C++ "standard" says something along
  // the lines of:
  //
  //   The function shall return the address of the start of a 
  //   block of storage whose length in bytes shall be at least
  //   as large as the requested size. There are no constraints
  //   on the contents of the allocated storage on return from 
  //   the allocation function. The order, contiguity, and initial
  //   value of storage allocated by successive calls to an 
  //   allocation function is unspecified. The pointer returned 
  //   shall be suitably aligned so that it can be converted to a 
  //   pointer of any complete object type and then used to
  //   access the object or array in the storage allocated (until
  //   the storage is explicitly deallocated by a call to a 
  //   corresponding deallocation function). If the size of the 
  //   space requested is zero, the value returned shall not be a
  //   null pointer value. The results of dereferencing a pointer
  //   returned as a request for zero size are undefined.
  
  return onmalloc(sz?sz:1);
}
Esempio n. 7
0
//////////
// If t is in the domain of the surface, GetSpanVectorIndex() returns the 
// span vector index "i" such that span_vector[i] <= t <= span_vector[i+1].
// The "side" parameter determines which span is selected when t is at the
// end of a span.
//
//virtual
ON_BOOL32 ON_Surface::GetSpanVectorIndex(
      int dir, // 0 gets first parameter's domain, 1 gets second parameter's domain
      double t,      // [IN] t = evaluation parameter
      int side,         // [IN] side 0 = default, -1 = from below, +1 = from above
      int* span_vector_i,        // [OUT] span vector index
      ON_Interval* span_domain // [OUT] domain of the span containing "t"
      ) const
{
  ON_BOOL32 rc = false;
  int i;
  int span_count = SpanCount(dir);
  if ( span_count > 0 ) {
    double* span_vector = (double*)onmalloc((span_count+1)*sizeof(span_vector[0]));
    rc = GetSpanVector( dir, span_vector );
    if (rc) {
      i = ON_NurbsSpanIndex( 2, span_count, span_vector, t, side, 0 );
      if ( i >= 0 && i <= span_count ) {
        if ( span_vector_i )
          *span_vector_i = i;
        if ( span_domain )
          span_domain->Set( span_vector[i], span_vector[i+1] );
      }
      else
        rc = false;
    }
    onfree(span_vector);
  }
  return rc;
}
Esempio n. 8
0
bool ON_CompressStream::Begin()
{
  if ( 0 != m_implementation )
  {
    onfree(m_implementation);
    m_implementation = 0;
  }

  // zero these because the same instance of an 
  // ON_CompressStream class may be used multiple times.
  m_in_size = 0;
  m_out_size = 0;
  m_in_crc = 0;
  m_out_crc = 0;

  struct ON_ZlibImplementation* imp = (struct ON_ZlibImplementation*)onmalloc(sizeof(*imp));
  memset(&imp->m_strm,0,sizeof(imp->m_strm));

  if ( Z_OK != deflateInit( &imp->m_strm, Z_BEST_COMPRESSION ) )
  {
    onfree(imp);
    return false;
  }

  m_implementation = imp;

  return true;
}
CRhinoCommand::result CCommandSampleTriangulatePolygon::RunCommand( const CRhinoCommandContext& context )
{
  CRhinoGetObject go;
  go.SetCommandPrompt( L"Select closed planar polygon to triangulate" );
  go.SetGeometryFilter( CRhinoGetObject::curve_object );
  go.SetGeometryFilter( CRhinoGetObject::closed_curve );
  go.EnableSubObjectSelect( FALSE );
  go.GetObjects( 1, 1 );
  if( go.CommandResult() != CRhinoCommand::success )
    return go.CommandResult();

  const CRhinoObjRef& ref = go.Object(0);

  ON_3dPointArray vertices;

  const ON_PolylineCurve* pc = ON_PolylineCurve::Cast( ref.Curve() );
  if( pc )
  {
    vertices = pc->m_pline;
  }
  else
  {
    const ON_NurbsCurve* nc = ON_NurbsCurve::Cast( ref.Curve() );
    if( nc )
      nc->IsPolyline( &vertices );
  }

  if( vertices.Count() < 5 )
  {
    RhinoApp().Print( L"Curve not polygon with at least four sides.\n" );
    return CRhinoCommand::nothing;
  }

  int* triangles = (int*)onmalloc( (vertices.Count()-3) * sizeof(int) * 3 );
  if( 0 == triangles )
    return CRhinoCommand::failure; // out of memory
  
  memset( triangles, 0, (vertices.Count()-3) * sizeof(int) * 3 );

  int rc = RhinoTriangulate3dPolygon( vertices.Count()-1, 3, (const double*)vertices.Array(), 3, triangles);
  if( 0 == rc )
  {
    int i;
    for( i = 0; i < vertices.Count()-3; i++ )
    {
      ON_Polyline pline;
      pline.Append( vertices[triangles[i * 3]] );
      pline.Append( vertices[triangles[i * 3 + 1]] );
      pline.Append( vertices[triangles[i * 3 + 2]] );
      pline.Append( pline[0] );
      context.m_doc.AddCurveObject( pline );
    }
    context.m_doc.Redraw();
  }

  onfree( triangles );

  return CRhinoCommand::success;
}
Esempio n. 10
0
int
ON_Matrix::RowReduce( 
    double zero_tolerance,
    ON_3dPoint* B,
    double* pivot 
    )
{
  ON_3dPoint t;
  double x, piv;
  int i, k, ix, rank;

  double** this_m = ThisM();
  piv = 0.0;
  rank = 0;
  const int n = m_row_count <= m_col_count ? m_row_count : m_col_count;
  for ( k = 0; k < n; k++ ) {
    //onfree( onmalloc( 1)); // 8-06-03 lw for cancel thread responsiveness
    onmalloc( 0); // 9-4-03 lw changed to 0
    ix = k;
    x = fabs(this_m[ix][k]);
    for ( i = k+1; i < m_row_count; i++ ) {
      if ( fabs(this_m[i][k]) > x ) {
        ix = i;
        x = fabs(this_m[ix][k]);
      }
    }
    if ( x < piv || k == 0 ) {
      piv = x;
    }
    if ( x <= zero_tolerance )
      break;
    rank++;

    // swap rows of matrix and B
    SwapRows( ix, k );
    t = B[ix]; B[ix] = B[k]; B[k] = t;

    // scale row k of matrix and B
    x = 1.0/this_m[k][k];
    this_m[k][k] = 1.0;
    ON_ArrayScale( m_col_count - 1 - k, x, &this_m[k][k+1], &this_m[k][k+1] );
    B[k] *= x;

    // zero column k for rows below this_m[k][k]
    for ( i = k+1; i < m_row_count; i++ ) {
      x = -this_m[i][k];
      this_m[i][k] = 0.0;
      if ( fabs(x) > zero_tolerance ) {
        ON_Array_aA_plus_B( m_col_count - 1 - k, x, &this_m[k][k+1], &this_m[i][k+1], &this_m[i][k+1] );
        B[i] += x*B[k];
      }
    }
  }

  if ( pivot )
    *pivot = piv;

  return rank;
}
Esempio n. 11
0
bool ON_SolveSVD(
  int row_count,
  int col_count,
  double const * const * U,
  const double* invW,
  double const * const * V,
  const double* B,
  double*& X
  )  
{
  int i, j;
  double *Y;
  const double* p0;
  double workY[128], x;

  if ( row_count < 1 || col_count < 1 || 0 == U || 0 == invW || 0 == V || 0 == B)
    return false;

  if ( 0 == X )
    X = (double*)onmalloc(col_count*sizeof(X[0]));
  Y = (col_count > 128)
    ? ( (double*)onmalloc(col_count*sizeof(*Y)) )
    : workY;
  for (i = 0; i < col_count; i++)
  {
    double y = 0.0;
    for (j = 0; j < row_count; j++)
      y += U[j][i] * *B++;
    B -= row_count;
    Y[i] = invW[i] * y;
  }
  for (i = 0; i < col_count; i++)
  {
    p0 = V[i];
    j = col_count;
    x = 0.0;
    while (j--)
      x += *p0++ * *Y++;
    Y -= col_count;
    X[i] = x;
  }
  if (Y != workY) 
    onfree(Y);

  return true;
}
Esempio n. 12
0
bool ON_CompressedBuffer::Read( ON_BinaryArchive& binary_archive )
{
  int major_version = 0;
  int minor_version = 0;
  bool rc = binary_archive.BeginRead3dmChunk(TCODE_ANONYMOUS_CHUNK,&major_version,&minor_version);
  if ( !rc )
    return false;

  for(;;)
  {
    rc = ( 1 == major_version );
    if ( !rc ) 
      break;
    rc = binary_archive.ReadSize(&m_sizeof_uncompressed);
    if (!rc)
      break;
    rc = binary_archive.ReadSize(&m_sizeof_compressed);
    if (!rc)
      break;
    rc = binary_archive.ReadInt(&m_crc_uncompressed);
    if (!rc)
      break;
    rc = binary_archive.ReadInt(&m_crc_compressed);
    if (!rc)
      break;
    rc = binary_archive.ReadInt(&m_method);
    if (!rc)
      break;
    rc = binary_archive.ReadInt(&m_sizeof_element);
    if (!rc)
      break;
    if ( m_sizeof_compressed > 0 )
    {
      m_buffer_compressed = onmalloc(m_sizeof_compressed);
      if ( m_buffer_compressed )
      {
        m_buffer_compressed_capacity = m_sizeof_compressed;
        rc = binary_archive.ReadByte(m_sizeof_compressed,m_buffer_compressed);
      }
      else
      {
        m_sizeof_compressed =0;
      }
      if (!rc)
        break;
    }

    break;
  }

  if ( !binary_archive.EndRead3dmChunk() )
    rc = false;

  return rc;
}
Esempio n. 13
0
ON_BOOL32
ON_Surface::Ev1Der( // returns false if unable to evaluate
       double s, double t, // evaluation parameters
       ON_3dPoint& point,
       ON_3dVector& ds,
       ON_3dVector& dt,
       int side,        // optional - determines which side to evaluate from
                       //         0 = default
                       //         1 from NE quadrant
                       //         2 from NW quadrant
                       //         3 from SW quadrant
                       //         4 from SE quadrant
       int* hint       // optional - evaluation hint (int[2]) used to speed
                       //            repeated evaluations
       ) const
{
  ON_BOOL32 rc = false;
  const int dim = Dimension();
  double ws[3*32];
  double* v;
  point.x = 0.0;
  point.y = 0.0;
  point.z = 0.0;
  ds.x = 0.0;
  ds.y = 0.0;
  ds.z = 0.0;
  dt.x = 0.0;
  dt.y = 0.0;
  dt.z = 0.0;
  if ( dim <= 32 ) {
    v = ws;
  }
  else {
    v = (double*)onmalloc(3*dim*sizeof(*v));
  }
  rc = Evaluate( s, t, 1, dim, v, side, hint );
  point.x = v[0];
  ds.x = v[dim];
  dt.x = v[2*dim];
  if ( dim > 1 ) {
    point.y = v[1];
    ds.y = v[dim+1];
    dt.y = v[2*dim+1];
    if ( dim > 2 ) {
      point.z = v[2];
      ds.z = v[dim+2];
      dt.z = v[2*dim+2];
      if ( dim > 32 )
        onfree(v);
    }
  }

  return rc;
}
Esempio n. 14
0
void ON_String::CopyToArray( int w_count, const wchar_t* w )
{
  // convert UTF-16 string to UTF-8 string
  int c_count = w2c_size( w_count, w );
  char* c = (char*)onmalloc(c_count+1);
  memset( c, 0, c_count+1 );
  const int c_length = w2c( w_count, w, c_count, c );
  c[c_length] = 0;
  CopyToArray( c_count, c );
  onfree(c);
}
Esempio n. 15
0
ON_wString::ON_wString( unsigned char c, int repeat_count )
{
  Create();
  if ( repeat_count > 0 ) {
    char* s = (char*)onmalloc((repeat_count+1)*sizeof(*s));
    s[repeat_count] = 0;
    memset( s, c, repeat_count*sizeof(*s) );
    CopyToArray( repeat_count, s );
    onfree(s);
    m_s[repeat_count] = 0;
    Header()->string_length = repeat_count;
  }
}
void ON_String::CreateArray( int capacity )
{
  Destroy();
  if ( capacity > 0 ) {
		ON_aStringHeader* p =
			(ON_aStringHeader*)onmalloc( sizeof(ON_aStringHeader) + (capacity+1)*sizeof(*m_s) );
		p->ref_count = 1;
		p->string_length = 0;
		p->string_capacity = capacity;
		m_s = p->string_array();
    memset( m_s, 0, (capacity+1)*sizeof(*m_s) );
  }
}
bool ON_BezierCage::Write(ON_BinaryArchive& archive) const
{
  bool rc = archive.BeginWrite3dmChunk(TCODE_ANONYMOUS_CHUNK,1,0);

  if (rc)
  {
    rc = archive.WriteInt(m_dim);
    if(rc)
      rc = archive.WriteInt(m_is_rat);
    if (rc)
      rc = archive.WriteInt(m_order[0]);
    if (rc)
      rc = archive.WriteInt(m_order[1]);
    if (rc)
      rc = archive.WriteInt(m_order[2]);
    int i,j,k;
    const int cv_dim = m_is_rat?(m_dim+1):m_dim;
    double* bogus_cv = 0;
    for(i = 0; i < m_order[0] && rc; i++)
    {
      for(j = 0; j < m_order[1] && rc; j++)
      {
        for ( k = 0; k < m_order[2] && rc; k++)
        {
          const double* cv = CV(i,j,k);
          if ( !cv )
          {
            if ( 0 == bogus_cv )
            {
              bogus_cv = (double*)onmalloc(cv_dim*sizeof(*bogus_cv));
              for ( int n = 0; n < cv_dim; n++ )
                bogus_cv[n] = ON_UNSET_VALUE;
            }
            cv = bogus_cv;
          }
          rc = archive.WriteDouble(cv_dim,cv);
        }
      }
    }

    if ( 0 != bogus_cv )
      onfree(bogus_cv);

    if ( !archive.EndWrite3dmChunk() )
    {
      rc = false;
    }
  }

  return rc;
}
Esempio n. 18
0
void* onmemdup( const void* src, size_t sz )
{
  void* p;
  if ( src && sz>0 )
  {
    p = onmalloc(sz);
    if (p)
      memcpy(p,src,sz);
  }
  else {
    p = 0;
  }
  return p;
}
Esempio n. 19
0
void
ON_hsort(void *base, size_t nel, size_t width, int (*compar)(const void*,const void*))
{
  size_t
    i_end,k;
  unsigned char
    work_memory[work_size], *e_tmp, *e_end;

  if (nel < 2) return;
  k = nel >> 1;
  i_end = nel-1;
  e_end = ((unsigned char*)base) + i_end*width;
  e_tmp = (width > work_size) ? (unsigned char*)onmalloc(width) : work_memory;
  for (;;) {
    if (k) {
      --k;
      memcpy(e_tmp,((unsigned char*)base)+k*width,width); /* e_tmp = e[k]; */
    } 
    else {      
      memcpy(e_tmp,e_end,width);     /* e_tmp = e[i_end]; */
      memcpy(e_end,base,width);      /* e[i_end] = e[0];  */
      if (!(--i_end)) {
        memcpy(base,e_tmp,width);    /* e[0] = e_tmp;     */
        break;
      }
      e_end -= width;
    }
    { size_t i, j;
      unsigned char *e_i, *e_j;
      i = k;
      j = (k<<1) + 1;
      e_i = ((unsigned char*)base) + i*width;
      while (j <= i_end) {
        e_j = ((unsigned char*)base) + j*width;
        if (j < i_end && compar(e_j,e_j+width)<0 /*e[j] < e[j + 1] */)
          {j++; e_j += width;}
        if (compar(e_tmp,e_j)<0 /* e_tmp < e[j] */) {
          memcpy(e_i,e_j,width); /* e[i] = e[j]; */
          i = j;
          e_i = e_j;
          j = (j<<1) + 1;
        } else j = i_end + 1;
      }
      memcpy(e_i,e_tmp,width); /* e[i] = e_tmp; */
    }
  }
  if (width > work_size) onfree(e_tmp); 
}
Esempio n. 20
0
void* onrealloc( void* memblock, size_t sz )
{
  if ( 0 == memblock )
  {
    return onmalloc(sz);
  }

  if ( 0 == sz )
  {
    onfree(memblock);
    return 0;
  }

#if defined(ON_REALLOC_BROKEN)
  /* use malloc() and memcpy() instead of buggy realloc() */
  void* p;
  const size_t memblocksz = _msize(memblock);
  if ( sz <= memblocksz ) {
    /* shrink */
    if ( memblocksz <= 28 || 8*sz >= 7*memblocksz ) 
    {
      /* don't bother reallocating */
      p = memblock;
    }
    else {
      /* allocate smaller block */
      p = malloc(sz);
      if ( p ) 
      {
        memcpy( p, memblock, sz );
        free(memblock);
      }
    }
  }
  else if ( sz > memblocksz ) {
    /* grow */
    p = malloc(sz);
    if ( p ) {
      memcpy( p, memblock, memblocksz );
      free(memblock);
    }
  }
  return p;
#else
  return realloc( memblock, sz );
#endif
}
Esempio n. 21
0
int ON_wString::Find( const char* s ) const
{
  int rc = -1;
  if ( s && s[0] && !IsEmpty() ) {
    const int s_count = (int)strlen(s); // the (int) is for 64 bit size_t conversion
    wchar_t* w = (wchar_t*)onmalloc( (s_count+2)*sizeof(w[0]) );
    c2w( s_count, s, s_count+1, w );
    const wchar_t* p;
    p = wcsstr( m_s, w );
    if ( p )
    {
      rc = ((int)(p-m_s)); // the (int) cast is for 64 bit compilers
    }
    onfree( w );
  }
  return rc;
}
Esempio n. 22
0
int ON_GetBase32Digits( const unsigned char* x, int x_count, unsigned char* base32_digits )
{
  int x_bit_count = 8*x_count;

  unsigned char mask, c;
  unsigned char bits[5] = {0,0,0,0,0};
  unsigned int bits_count = 0;
  unsigned int base32_digit_count = 0;
  int i;

  if ( 0 == base32_digits || 0 == x || x_count <= 0 )
    return 0;

  if ( x == base32_digits )
  {
    unsigned char* tmp = (unsigned char*)onmalloc(x_count*sizeof(x[0]));
    if ( 0 == tmp )
      return 0;
    memcpy(tmp,x,x_count*sizeof(x[0]));
    i = ON_GetBase32Digits(tmp,x_count,base32_digits);    
    onfree(tmp);
    return i;
  }

  i = x_bit_count % 5;
  if ( i )
    bits_count = 5-i;

  for ( i = 0; i < x_count; i++)
  {
    c = x[i];
    for (mask = 128; 0 != mask; mask /= 2 )
    {
      bits[bits_count++] = (0 != (c & mask)) ? 1 : 0;
      if ( 5 == bits_count )
      {
        base32_digits[base32_digit_count++] = 16*bits[0] + 8*bits[1] + 4*bits[2] + 2*bits[3] + bits[4];
        bits_count = 0;
      }
    }
  }

  return base32_digit_count;
}
/////////////////////////////////////////////////////////////////
// Tools for managing CV and knot memory
bool ON_BezierCage::ReserveCVCapacity(
  int capacity// number of doubles to reserve
  )
{
  if ( m_cv_capacity < capacity ) {
    if ( m_cv ) {
      if ( m_cv_capacity ) {
        m_cv = (double*)onrealloc( m_cv, capacity*sizeof(*m_cv) );
        m_cv_capacity = (m_cv) ? capacity : 0;
      }
      // else user supplied m_cv[] array
    }
    else {
      m_cv = (double*)onmalloc( capacity*sizeof(*m_cv) );
      m_cv_capacity = (m_cv) ? capacity : 0;
    }
  }
  return ( m_cv ) ? true : false;
}
Esempio n. 24
0
void ON_BumpFunction::Evaluate(double s, double t, int der_count, double* value) const
{
    double tmp[20];
    double* xvalue;
    double* yvalue;
    xvalue = ( der_count > 9 )
             ? ((double*)onmalloc((der_count+1)*2*sizeof(xvalue[0])))
             : &tmp[0];
    yvalue = xvalue + (der_count+1);
    double x = s-m_x0;
    const double dx = m_sx[x >= 0.0 ? 1 : 0];
    x *= dx;
    double y = t-m_y0;
    const double dy = m_sy[y >= 0.0 ? 1 : 0];
    y *= dy;

    if ( 5 == m_type[0] )
    {
        EvaluateHelperQuinticBump(x,dx,der_count,xvalue);
    }
    else
    {
        EvaluateHelperLinearBump(x,dx,der_count,xvalue);
    }
    if ( 5 == m_type[1] )
    {
        EvaluateHelperQuinticBump(y,dy,der_count,yvalue);
    }
    else
    {
        EvaluateHelperLinearBump(y,dy,der_count,yvalue);
    }

    int n, i, j;
    for ( n = 0; n <= der_count; n++ )
    {
        for ( i = n, j = 0; j <= n; i--, j++ )
        {
            *value++ = m_a*xvalue[i]*yvalue[j]; // d^nf/(ds^i dt^j)
        }
    }
}
Esempio n. 25
0
int ON_InvertSVDW(
  int count, 
  const double* W,
  double*& invW
  )
{
  double w, maxw;
  int i;

  if ( 0 == W || count <= 0 )
    return -1;

  if ( 0 == invW )
  {
    invW = (double*)onmalloc(count*sizeof(invW[0]));
  }
  maxw = fabs(W[0]);
  for (i = 1; i < count; i++) {
    w = fabs(W[i]);
    if (w > maxw) maxw = w;
  }
  if (maxw == 0.0)
  {
    if ( W != invW )
      memset(invW,0,count*sizeof(invW[0]));
    return 0;
  }

  i = 0;
  maxw *= ON_SQRT_EPSILON;
  while (count--) 
  {
    if (fabs(W[count]) > maxw) 
    {
      i++;
      invW[count] = 1.0/W[count];
    }
    else
      invW[count] = 0.0;
  }
  return i; // number of nonzero terms in invW[]
}
Esempio n. 26
0
int ON_wString::CompareNoCase( const char* s) const
{
  int rc = 0;
  if ( s && s[0] ) {
    if ( IsEmpty() ) {
      rc = -1;
    }
    else {
      int c_count = w2c_size( Length(m_s), m_s );
      char* c = (char*)onmalloc((c_count+1)*sizeof(*c));
      w2c( Length(m_s), m_s, c_count, c );
      c[c_count] = 0;
      rc = on_stricmp( c, s );
      onfree(c);
    }
  }
  else {
    rc = IsEmpty() ? 0 : 1;
  }
  return rc;
}
Esempio n. 27
0
ON_BOOL32 
ON_Surface::EvPoint( // returns false if unable to evaluate
       double s, double t, // evaluation parameters
       ON_3dPoint& point,
       int side,        // optional - determines which side to evaluate from
                       //         0 = default
                       //         1 from NE quadrant
                       //         2 from NW quadrant
                       //         3 from SW quadrant
                       //         4 from SE quadrant
       int* hint       // optional - evaluation hint (int[2]) used to speed
                       //            repeated evaluations
       ) const
{
  ON_BOOL32 rc = false;
  double ws[128];
  double* v;
  if ( Dimension() <= 3 ) {
    v = &point.x;
    point.x = 0.0;
    point.y = 0.0;
    point.z = 0.0;
  }
  else if ( Dimension() <= 128 ) {
    v = ws;
  }
  else {
    v = (double*)onmalloc(Dimension()*sizeof(*v));
  }
  rc = Evaluate( s, t, 0, Dimension(), v, side, hint );
  if ( Dimension() > 3 ) {
    point.x = v[0];
    point.y = v[1];
    point.z = v[2];
    if ( Dimension() > 128 )
      onfree(v);
  }
  return rc;
}
Esempio n. 28
0
bool ON_OffsetSurfaceFunction::EvaluateDistance(
    double s,
    double t,
    int num_der,
    double* value
) const
{
    const int vcnt = ((num_der+1)*(num_der+2))/2;
    int vi;
    for ( vi = 0; vi < vcnt; vi++ )
    {
        value[vi] = 0;
    }

    bool rc = const_cast<ON_OffsetSurfaceFunction*>(this)->Initialize();

    if (rc)
    {
        double barray[21];
        double* bump_value = (vcnt > 21)
                             ? (double*)onmalloc(vcnt*sizeof(bump_value[0]))
                             : barray;
        const int bump_count = m_bumps.Count();
        int bump_index, vi;
        for ( bump_index = 0; bump_index < bump_count; bump_index++ )
        {
            m_bumps[bump_index].Evaluate( s, t, num_der, bump_value );
            for ( vi = 0; vi < vcnt; vi++ )
            {
                value[vi] += bump_value[vi];
            }
        }
        if ( bump_value != barray )
            onfree(bump_value);
    }
    return rc;
}
Esempio n. 29
0
ON_CompressedBuffer& ON_CompressedBuffer::operator=(const ON_CompressedBuffer& src)
{
  if ( this != &src )
  {
    Destroy();
    if( src.m_buffer_compressed && src.m_sizeof_compressed > 0 )
    {
      m_sizeof_uncompressed = src.m_sizeof_uncompressed;
      m_sizeof_compressed   = src.m_sizeof_compressed;
      m_crc_uncompressed    = src.m_crc_uncompressed;
      m_crc_compressed      = src.m_crc_compressed;
      m_method              = src.m_method;
      m_sizeof_element      = src.m_sizeof_element;

      m_buffer_compressed = onmalloc(m_sizeof_compressed);
      if( m_buffer_compressed )
      {
        m_buffer_compressed_capacity = m_sizeof_compressed;
        memcpy(m_buffer_compressed,src.m_buffer_compressed,m_sizeof_compressed);
      }
    }
  }
  return *this;
}
Esempio n. 30
0
/*
Returns:
  Height of the 'I' character when the font is drawn 
  with m_logfont.lfHeight = 256.
*/
int ON_Font::HeightOfI() const
{
  if ( m_I_height  <= 0 )
  {
    // Default is height of Arial 'I'.  If we are running
    // on Windows, then we calculate the actual height of
    // an 'I' in the font.
    //   The ..ON_Font::normal_font_height/256 is here 
    //   so this code will continue to work correctly 
    //   if somebody changes ON_Font::normal_font_height.
    int I_height = (166*ON_Font::normal_font_height)/256;

#if defined(ON_OS_WINDOWS_GDI)
    if ( m_logfont.lfFaceName[0] )
    {
      // Get the height of an 'I'
      HDC hdc = ::GetDC( NULL);
      if (hdc)
      {
        LOGFONT logfont = m_logfont;
        logfont.lfHeight = normal_font_height;
        HFONT font = ::CreateFontIndirect( &logfont);
        if ( font )
        {
          wchar_t str[2];
          str[0] = ON_Font::m_metrics_char;
          str[1] = 0;
          HFONT oldfont = (HFONT)::SelectObject( hdc, font);
          ::SetBkMode( hdc, TRANSPARENT);
          ::BeginPath(hdc);
          ::ExtTextOut( hdc, 0, 0, 0, NULL, str, 1, NULL);
          ::EndPath( hdc);
          int numPoints = ::GetPath( hdc, NULL, NULL, 0);

          if( numPoints > 2)
          {
            // Allocate room for the points & point types
            LPPOINT pPoints = (LPPOINT)onmalloc( numPoints * sizeof(*pPoints) );
            LPBYTE pTypes = (LPBYTE)onmalloc( numPoints * sizeof(*pTypes) );
            if ( pTypes && pPoints)
            {
              // Get the points and types from the current path
              numPoints = ::GetPath( hdc, pPoints, pTypes, numPoints);
              if( numPoints > 2)
              {
                int ymin = pPoints[0].y;
                int ymax = ymin;
                int k;
                for( k = 1; k < numPoints; k++)
                {
                  if( pPoints[k].y < ymin)
                    ymin = pPoints[k].y;
                  else if( pPoints[k].y > ymax)
                    ymax = pPoints[k].y;
                }
                I_height = ymax - ymin + 1;
              }
            }
            onfree( pPoints);
            onfree( pTypes);
          }
          ::SelectObject( hdc, oldfont);
          ::DeleteObject( font);
        }
      }
      ::ReleaseDC( NULL, hdc);
    }
#endif
    const_cast<ON_Font*>(this)->m_I_height = I_height;
  }
  return m_I_height;
}