NodeControlViewImpl::NodeControlViewImpl(collada::Node *node, collada::Scene *context)
  : m_node(node)
  , m_coi(0,0,0)
  , m_context(context)
{
	assert(m_node);
	float det = MatrixDeterminant((const float4x4 *) m_node->GetWorld(m_context));
	m_scale = 1.0f / pow(det, 1.0f/3);
	m_transform = m_node->AppendTransform(collada::Transform::MATRIX, I)->GetDelegate(NULL);
}
Exemplo n.º 2
0
task main()
{
  // Initiate BNS Library
  BNS();

  // Create a 3x3 matrix of zeros
  Matrix mat1;
  CreateZerosMatrix(&mat1, 3, 3);

  // Create a 3x3 matrix with some data in it
  // Set location 1 down, 0 across, to be 16
  Matrix mat2;
  CreateMatrix(&mat2, "1.10 3.40 0; 5 3 2; 0 1 1.234");
  SetMatrixAt(&mat2, 1, 0, 16);

  // Creates a 3x3 identity matrix, then multiply it by 11
  Matrix mat3;
  CreateIdentityMatrix(&mat3, 3);
  MatrixMultiplyScalar(&mat3, 11);

  // Print matricies to the debugger console
  PrintMatrix(&mat1);
  PrintMatrix(&mat2);
  PrintMatrix(&mat3);

  // Matrix Examples:

  // Matrix determinant
  float det = MatrixDeterminant(&mat2);
  writeDebugStreamLine("Matrix Det = %f", det);

  // Matrix Inverse
  Matrix inv;
  MatrixInv(&inv, mat2);
  writeDebugStream("Inverse ");
  PrintMatrix(&inv);

  // Matrix Multiplication
  Matrix mult;
  MatrixMult(&mult, mat2, mat3);
  writeDebugStream("Multiply ");
  PrintMatrix(mult);

  // Matrix Addition
  Matrix add;
  MatrixAdd(&add, mat2, mat3);
  writeDebugStream("Add ");
  PrintMatrix(&add);

  // Matrix Subtraction
  Matrix sub;
  MatrixSub(&sub, mat3, mat2);
  writeDebugStream("Subtract ");
  PrintMatrix(&sub);
}
Exemplo n.º 3
0
void
MatrixTest::TestMatrixDeterminant()
{
    double tolerance = 1e-4;

    std::cout << "\rMatrixTest::TestNRMatrixDeterminant()\n";

    CPPUNIT_ASSERT_DOUBLES_EQUAL( (double)MatrixDeterminant(mPascalMatrix),
                                  1.0,
                                  tolerance);

    CPPUNIT_ASSERT_DOUBLES_EQUAL( (double)MatrixDeterminant(mZeroesMatrix),
                                  0.0,
                                  tolerance );

    CPPUNIT_ASSERT_DOUBLES_EQUAL( (double)MatrixDeterminant(mIdentityMatrix),
                                  1.0,
                                  tolerance );

    CPPUNIT_ASSERT_DOUBLES_EQUAL( (double)MatrixDeterminant(mOneMatrix),
                                  5.0,
                                  tolerance );

    CPPUNIT_ASSERT_DOUBLES_EQUAL( (double)MatrixDeterminant(mSingularMatrix),
                                  0.0,
                                  tolerance );

    // the tolerance had to be increased for this test case to pass.
    // The determinant is much larger in this case.
    const double buckyTolerance = 4;
    CPPUNIT_ASSERT_DOUBLES_EQUAL( (double)MatrixDeterminant(mBuckyMatrix),
                                  2985984.0,
                                  buckyTolerance );

    // non-square matrices will have a determinant of 0 for us
    CPPUNIT_ASSERT_DOUBLES_EQUAL( (double)MatrixDeterminant(mNonSquareMatrix),
                                  0.0,
                                  tolerance );

    CPPUNIT_ASSERT_DOUBLES_EQUAL( (double)MatrixDeterminant(mOneSmallMatrix),
                                  2.e-20,
                                  tolerance );
}
Exemplo n.º 4
0
/**
 * MatrixInverse calculates the inverse of a matrix and returns the
 * result through the second argument.
 */
void MatrixInverse(float mat[3][3], float result[3][3])
{
 //   float a, b, c, d, e, f, g, h, i;
    float det = 0;
    det = MatrixDeterminant(mat);
    det = 1 / det;
/*    a = mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1];
    b = mat[0][2] * mat[2][1] - mat[2][2] * mat[0][1];
    c = mat[0][1] * mat[1][2] - mat[1][1] * mat[0][2];
    d = mat[1][2] * mat[2][0] - mat[2][2] * mat[1][0];
    e = mat[0][0] * mat[2][2] - mat[0][2] * mat[2][0];
    f = mat[0][2] * mat[1][0] - mat[0][0] * mat[1][2];
    g = mat[1][0] * mat[2][1] - mat[2][0] * mat[1][1];
    h = mat[0][1] * mat[2][0] - mat[2][1] * mat[0][0];
    i = mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];
    float mat1[3][3] = {
        {a, b, c},
        {d, e, f},
        {g, h, i}
    };  */
    MatrixAdjugate(mat,result);
    MatrixScalarMultiply(det, result, result);
}
Exemplo n.º 5
0
static int
compute_cluster_statistics(MRI_SURFACE *mris, MRI *mri_profiles, MATRIX **m_covs, VECTOR **v_means, int k) {
  int    i, vno, cluster, nsamples, num[MAX_CLUSTERS];
  int    singular, cno_pooled, cno ;
  MATRIX *m1, *mpooled, *m_inv_covs[MAX_CLUSTERS] ;
  VECTOR *v1 ;
  FILE   *fp ;
  double det, det_pooled ;

  memset(num, 0, sizeof(num)) ;
  nsamples = mri_profiles->nframes ;

  v1 = VectorAlloc(nsamples, MATRIX_REAL) ;
  m1 = MatrixAlloc(nsamples, nsamples, MATRIX_REAL) ;
  mpooled = MatrixAlloc(nsamples, nsamples, MATRIX_REAL) ;

  for (cluster = 0 ; cluster < k ; cluster++) {
    VectorClear(v_means[cluster]) ;
    MatrixClear(m_covs[cluster]) ;
  }

  // compute means
  // fp = fopen("co.dat", "w") ;
  fp = NULL ;
  for (vno = 0 ; vno < mris->nvertices ; vno++) {
    cluster = mris->vertices[vno].curv ;
    for (i = 0 ; i < nsamples ; i++) {
      VECTOR_ELT(v1, i+1) = MRIgetVoxVal(mri_profiles, vno, 0, 0, i) ;
      if (cluster == 0 && fp)
        fprintf(fp, "%f ", VECTOR_ELT(v1, i+1));
    }
    if (cluster == 0 && fp)
      fprintf(fp, "\n") ;
    num[cluster]++ ;
    VectorAdd(v_means[cluster], v1, v_means[cluster]) ;
  }

  if (fp)
    fclose(fp) ;
  for (cluster = 0 ; cluster < k ; cluster++)
    if (num[cluster] > 0)
      VectorScalarMul(v_means[cluster], 1.0/(double)num[cluster], v_means[cluster]) ;

  // compute inverse covariances
  for (vno = 0 ; vno < mris->nvertices ; vno++) {
    cluster = mris->vertices[vno].curv ;
    for (i = 0 ; i < nsamples ; i++)
      VECTOR_ELT(v1, i+1) = MRIgetVoxVal(mri_profiles, vno, 0, 0, i) ;
    VectorSubtract(v_means[cluster], v1, v1) ;
    VectorOuterProduct(v1, v1, m1) ;
    MatrixAdd(m_covs[cluster], m1, m_covs[cluster]) ;
    MatrixAdd(mpooled, m1, mpooled) ;
  }

  MatrixScalarMul(mpooled, 1.0/(double)mris->nvertices, mpooled) ;
  cno_pooled = MatrixConditionNumber(mpooled) ;
  det_pooled = MatrixDeterminant(mpooled) ;
  for (cluster = 0 ; cluster < k ; cluster++)
    if (num[cluster] > 0)
      MatrixScalarMul(m_covs[cluster], 1.0/(double)num[cluster], m_covs[cluster]) ;


  // invert all the covariance matrices
  MatrixFree(&m1) ;
  singular = 0 ;
  for (cluster = 0 ; cluster < k ; cluster++) {
    m1 = MatrixInverse(m_covs[cluster], NULL) ;
    cno = MatrixConditionNumber(m_covs[cluster]) ;
    det = MatrixDeterminant(m_covs[cluster]) ;
    if (m1 == NULL)
      singular++ ;
    while (cno > 100*cno_pooled || 100*det < det_pooled) {
      if (m1)
        MatrixFree(&m1) ;
      m1 = MatrixScalarMul(mpooled, 0.1, NULL) ;
      MatrixAdd(m_covs[cluster], m1, m_covs[cluster]) ;
      MatrixFree(&m1) ;
      cno = MatrixConditionNumber(m_covs[cluster]) ;
      m1 = MatrixInverse(m_covs[cluster], NULL) ;
      det = MatrixDeterminant(m_covs[cluster]) ;
    }
    m_inv_covs[cluster] = m1 ;
  }

  for (cluster = 0 ; cluster < k ; cluster++) {
    if (m_inv_covs[cluster] == NULL)
      DiagBreak() ;
    else {
      MatrixFree(&m_covs[cluster]) ;
      m_covs[cluster] = m_inv_covs[cluster] ;
      //   MatrixIdentity(m_covs[cluster]->rows, m_covs[cluster]);
    }
  }
  MatrixFree(&mpooled) ;
  VectorFree(&v1) ;
  return(NO_ERROR) ;
}
Exemplo n.º 6
0
int
main(int argc, char *argv[])
{
  char         **av, *out_name ;
  int          ac, nargs ;
  int          msec, minutes, seconds ;
  struct timeb start ;
  GCA_MORPH    *gcam ;
  MRI          *mri = NULL ;
  MATRIX       *m;

  /* rkt: check for and handle version tag */
  nargs = handle_version_option (argc, argv, "$Id: mri_warp_convert.c,v 1.1 2012/02/13 23:05:52 jonp Exp $", "$Name:  $");
  if (nargs && argc - nargs == 1)
  {
    exit (0);
  }
  argc -= nargs;

  Progname = basename(argv[0]) ;
  ErrorInit(NULL, NULL, NULL) ;
  DiagInit(NULL, NULL, NULL) ;

  TimerStart(&start) ;

  ac = argc ;
  av = argv ;
  for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++)
  {
    nargs = get_option(argc, argv) ;
    argc -= nargs ;
    argv += nargs ;
  }

  if (argc < 3)
  {
    usage_exit(1) ;
  }

  // mri_convert expects a relative warp
  fprintf(stdout, "[%s]:  reading warp file '%s'\n", Progname, argv[1]);
  fprintf(stdout, "assuming RELATIVE warp convention\n");

  // TODO: add support for absolute warps as well, add option to specify convention


  mri = MRIread(argv[1]) ;
  if (mri == NULL)
    ErrorExit(ERROR_NOFILE, "%s: could not read warp volume %s\n", Progname,argv[1]) ;

  m = MRIgetVoxelToRasXform(mri) ;

  // NOTE: this assumes a standard siemens image orientation in which
  // case a neurological orientation means that the first frame is
  // flipped

  if ( MatrixDeterminant(m) > 0 )
    {
      fprintf(stdout, "non-negative Jacobian determinant -- converting to radiological ordering\n");
    }
  {
    // 2012/feb/08: tested with anisotropic voxel sizes

    MRI *mri2 = NULL ;
    int c=0,r=0,s=0;
    float v;

    mri2 = MRIcopy(mri,NULL);
    for(c=0; c < mri->width; c++)
      {
        for(r=0; r < mri->height; r++)
          {
            for(s=0; s < mri->depth; s++)
              {
                // only flip first frame (by negating relative shifts)
                v = MRIgetVoxVal(mri, c,r,s,0) / mri->xsize;
                if ( MatrixDeterminant(m) > 0 )
                  MRIsetVoxVal(    mri2,c,r,s,0,-v);
                else
                  MRIsetVoxVal(    mri2,c,r,s,0, v);

                v = MRIgetVoxVal(mri, c,r,s,1) / mri->ysize;
                MRIsetVoxVal(    mri2,c,r,s,1, v);

                v = MRIgetVoxVal(mri, c,r,s,2) / mri->zsize;
                MRIsetVoxVal(    mri2,c,r,s,2, v);

              }
          }
      }
    MRIfree(&mri);
    mri = mri2;

  }
  MatrixFree(&m) ;


  // this does all the work! (gcamorph.c)
  gcam = GCAMalloc(mri->width, mri->height, mri->depth) ;
  GCAMinitVolGeom(gcam, mri, mri) ;

  // not sure if removing singularities is ever a bad thing
#if 1
  GCAMremoveSingularitiesAndReadWarpFromMRI(gcam, mri) ;
#else
  GCAMreadWarpFromMRI(gcam, mri) ;
#endif

  fprintf(stdout, "[%s]:  writing warp file '%s'\n", Progname, argv[2]);

  out_name = argv[2] ;
  GCAMwrite(gcam, out_name) ;

  msec = TimerStop(&start) ;
  seconds = nint((float)msec/1000.0f) ;
  minutes = seconds / 60 ;
  seconds = seconds % 60 ;
  fprintf(stderr, "conversion took %d minutes and %d seconds.\n", minutes, seconds) ;
  exit(0) ;
  return(0) ;
}
Exemplo n.º 7
0
int
main(int argc, char *argv[])
{
  char      **av, *out_fname ;
  int       ac, nargs ;
  GCA_MORPH *gcam ;
  int       msec, minutes, seconds ;
  struct timeb start ;
  MRI       *mri, *mri_jacobian, *mri_area, *mri_orig_area ;

  /* rkt: check for and handle version tag */
  nargs = handle_version_option (argc, argv, "$Id: mri_jacobian.c,v 1.11 2011/12/10 22:47:57 fischl Exp $", "$Name:  $");
  if (nargs && argc - nargs == 1)
    exit (0);
  argc -= nargs;

  Progname = argv[0] ;
  ErrorInit(NULL, NULL, NULL) ;
  DiagInit(NULL, NULL, NULL) ;

  TimerStart(&start) ;

  ac = argc ;
  av = argv ;
  for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++)
    {
      nargs = get_option(argc, argv) ;
      argc -= nargs ;
      argv += nargs ;
    }

  if (argc < 4)
    usage_exit(1) ;

  out_fname = argv[argc-1] ;
  gcam = GCAMread(argv[1]) ;
  
  if (gcam == NULL)
    ErrorExit(ERROR_BADPARM, "%s: could not read input morph %s\n", Progname,argv[1]);
  if (Gx >= 0 && atlas == 0)
    find_debug_node(gcam, Gx, Gy, Gz) ;

  mri = MRIread(argv[2]) ;
  if (gcam == NULL)
    ErrorExit(ERROR_BADPARM, "%s: could not read template volume %s\n", Progname,argv[2]);

  GCAMrasToVox(gcam, mri) ;
  if (init || tm3dfile)
    init_gcam_areas(gcam) ;
  if (atlas)
    {
      mri_area = GCAMwriteMRI(gcam, NULL, GCAM_AREA);
      mri_orig_area = GCAMwriteMRI(gcam, NULL, GCAM_ORIG_AREA);
    }
  else
    {
      mri_area = GCAMmorphFieldFromAtlas(gcam, mri, GCAM_AREA, 0, 0);
      mri_orig_area = GCAMmorphFieldFromAtlas(gcam, mri, GCAM_ORIG_AREA, 0, 0);
    }

  if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON)
    {
      MRIwrite(mri_orig_area, "o.mgz") ;
      MRIwrite(mri_area, "a.mgz") ;
    }
  if (Gx > 0)
    printf("area = %2.3f, orig = %2.3f\n", 
	   MRIgetVoxVal(mri_area, Gx, Gy, Gz,0),MRIgetVoxVal(mri_orig_area,Gx,Gy,Gz,0)) ;
  if (lta)
    {
      double det ;
      if  (lta->type == LINEAR_RAS_TO_RAS)
	LTArasToVoxelXform(lta, mri, mri) ;
      det = MatrixDeterminant(lta->xforms[0].m_L) ;
      printf("correcting transform with det=%2.3f\n", det) ;
      MRIscalarMul(mri_orig_area, mri_orig_area, 1/det) ;
    }
  
  if (! FZERO(sigma))
    {
      MRI *mri_kernel, *mri_smooth ;
      mri_kernel = MRIgaussian1d(sigma, 100) ;
      mri_smooth = MRIconvolveGaussian(mri_area, NULL, mri_kernel) ;
      MRIfree(&mri_area) ; mri_area = mri_smooth ;
      mri_smooth = MRIconvolveGaussian(mri_orig_area, NULL, mri_kernel) ;
      MRIfree(&mri_orig_area) ; mri_orig_area = mri_smooth ;

      MRIfree(&mri_kernel) ; 
    }
  if (Gx > 0)
    printf("after smoothing area = %2.3f, orig = %2.3f\n", 
	   MRIgetVoxVal(mri_area, Gx, Gy, Gz,0),MRIgetVoxVal(mri_orig_area,Gx,Gy,Gz,0)) ;
  mri_jacobian = MRIdivide(mri_area, mri_orig_area, NULL) ;
  if (Gx > 0)
    printf("jacobian = %2.3f\n", MRIgetVoxVal(mri_jacobian, Gx, Gy, Gz,0)) ;
  if (atlas)
    mask_invalid(gcam, mri_jacobian) ;
  if (use_log)
    {
      MRIlog10(mri_jacobian, NULL, mri_jacobian, 0) ;
      if (zero_mean)
	MRIzeroMean(mri_jacobian, mri_jacobian) ;
      if (Gx > 0)
	printf("log jacobian = %2.3f\n", MRIgetVoxVal(mri_jacobian, Gx, Gy, Gz,0)) ;
    }
  fprintf(stderr, "writing to %s...\n", out_fname) ;
  MRIwrite(mri_jacobian, out_fname) ;
  MRIfree(&mri_jacobian) ;
  if (write_areas)
    {
      char fname[STRLEN] ;
      sprintf(fname, "%s_area.mgz", out_fname) ;
      printf("writing area to %s\n", fname) ;
      MRIwrite(mri_area, fname) ;
      sprintf(fname, "%s_orig_area.mgz", out_fname) ;
      printf("writing orig area to %s\n", fname) ;
      MRIwrite(mri_orig_area, fname) ;
    }
  if (atlas && DIAG_WRITE && DIAG_VERBOSE_ON)
    {
      char fname[STRLEN] ;
      FileNameRemoveExtension(out_fname, out_fname) ;
      mri_area = GCAMwriteMRI(gcam, mri_area, GCAM_MEANS);
      sprintf(fname, "%s_means.mgz", out_fname) ;
      printf("writing means to %s\n", fname) ;
      MRIwrite(mri_area, fname) ;
      sprintf(fname, "%s_labels.mgz", out_fname) ;
      mri_area = GCAMwriteMRI(gcam, mri_area, GCAM_LABEL);
      printf("writing labels to %s\n", fname) ;
      MRIwrite(mri_area, fname) ;
    }
  msec = TimerStop(&start) ;
  seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ;seconds = seconds % 60 ;
  fprintf(stderr, "jacobian calculation took %d minutes and %d seconds.\n", minutes, seconds) ;
  exit(0) ;
  return(0) ;
}