void _lucIsosurface_Write( void* drawingObject, lucDatabase* database, Bool walls )
{
   /* Export surface triangles */
   lucIsosurface* self = (lucIsosurface*)drawingObject;
   Index triangle_I;
   int i;
   for ( triangle_I = 0 ; triangle_I < self->triangleCount ; triangle_I++)
   {
      if (self->triangleList[triangle_I].wall != walls) continue;
      for (i=0; i<3; i++)
      {
         /* Dump vertex pos, [value] */
         float coordf[3] = {self->triangleList[triangle_I].pos[i][0],
                            self->triangleList[triangle_I].pos[i][1],
                            self->triangleList[triangle_I].pos[i][2]};
         float value = self->triangleList[triangle_I].value[i];
         lucDatabase_AddVertices(database, 1, lucTriangleType, coordf);
         if (self->colourField && self->colourMap)
            lucDatabase_AddValues(database, 1, lucTriangleType, lucColourValueData, self->colourMap, &value);
      }
   }
}
void _lucVectorArrowCrossSection_DrawCrossSection( void* drawingObject, lucDatabase* database, Dimension_Index dim )
{
   lucVectorArrowCrossSection*  self           = (lucVectorArrowCrossSection*)drawingObject;
   FieldVariable*    vectorVariable = self->fieldVariable;
   double            min = 0.0, max = self->maximum;
   Index          aIndex, bIndex;

   Journal_Firewall( vectorVariable->fieldComponentCount == vectorVariable->dim, lucError,
                     "Error - in %s(): provided FieldVariable \"%s\" has %u components - but %s Component "
                     "can only visualse FieldVariables with %d components.\n", __func__, vectorVariable->name,
                     vectorVariable->fieldComponentCount, self->type, vectorVariable->dim );

   if ( True == self->dynamicRange )
   {
      min = FieldVariable_GetMinGlobalFieldMagnitude( vectorVariable );
      max = FieldVariable_GetMaxGlobalFieldMagnitude( vectorVariable );
   }

   /* Force 3d vectors */
   lucCrossSection_AllocateSampleData(self, 3);

   /* Sample the 2d cross-section */
   lucCrossSection_SampleField(self, False);

   /* Write only values that have data on this processor! */
   for ( aIndex = 0 ; aIndex < self->resolutionA ; aIndex++ )
   {
      for ( bIndex = 0 ; bIndex < self->resolutionB ; bIndex++ )
      {
         if (self->values[aIndex][bIndex][0] != HUGE_VAL)
         {
            lucDatabase_AddVertices(database, 1, lucVectorType, &self->vertices[aIndex][bIndex][0]);
            lucDatabase_AddVectors(database, 1, lucVectorType, min, max, &self->values[aIndex][bIndex][0]);
         }
      }
   }

   lucCrossSection_FreeSampleData(self);
}
void lucContourCrossSection_PlotPoint(lucContourCrossSection* self, lucDatabase* database, char edge, double isovalue, int aIndex, int bIndex)
{
   Coord vertex;
   double aPos = (double)aIndex;
   double bPos = (double)bIndex;
   double leftBtm = self->values[aIndex][bIndex][0];
   double rightBtm = self->values[aIndex+1][bIndex][0];
   double leftTop = self->values[aIndex][bIndex+1][0];
   double rightTop = self->values[aIndex+1][bIndex+1][0];

   switch (edge)
   {
   case BOTTOM:
      aPos += (isovalue - leftBtm)/(rightBtm - leftBtm);
      break;
   case TOP:
      aPos += (isovalue - leftTop)/(rightTop - leftTop);
      bPos += 1.0;
      break;
   case LEFT:
      bPos += (isovalue - leftBtm)/(leftTop - leftBtm);
      break;
   case RIGHT:
      aPos += 1.0;
      bPos += (isovalue - rightBtm)/(rightTop - rightBtm);
      break;
   }

   lucCrossSection_Interpolate2d(self, aPos / (double)(self->resolutionA-1), bPos / (double)(self->resolutionB-1), vertex);

   /* Dump vertex pos */
   float pos[3] = {vertex[0], vertex[1], vertex[2]};
   float value = isovalue;
   lucDatabase_AddVertices(database, 1, lucLineType, pos);
   if (self->colourMap)
      lucDatabase_AddValues(database, 1, lucLineType, lucColourValueData, self->colourMap, &value);

   if (self->showValues && self->coordIndex % 4 == edge) 
   { 
      if (self->printedIndex < self->coordIndex && (0 == bIndex || 0 == aIndex || bIndex == (self->resolutionB-1) || aIndex == (self->resolutionA-1)))
      {
         char label[32];
         double dimCoeff = 1.0; /* coefficient for dimensionalising field */
         //TODO: Fix scaling/units
//         /* very hacky to get the scaling component */
//         Scaling* theScaling = NULL;
//         if (self->fieldVariable->context) theScaling = self->fieldVariable->context->scaling;
//         if (self->fieldVariable->o_units && theScaling)
//            dimCoeff = Scaling_ParseDimCoeff( theScaling, self->fieldVariable->o_units );
//
         /* Add the vertex for the label as a point */
         lucDatabase_AddVertices(database, 1, lucPointType, pos);
         /* Add to the label data */
         sprintf(label, " %g", isovalue * dimCoeff);
         //sprintf(label, " %g%s", isovalue * dimCoeff,
         //         self->printUnits && self->fieldVariable->o_units ? self->fieldVariable->o_units : "");
         lucDatabase_AddLabel(database, lucPointType, label);

         self->printedIndex = self->coordIndex;
      }
   }
}
void _lucEigenvectorsCrossSection_DrawCrossSection( void* drawingObject, lucDatabase* database, Dimension_Index dim )
{
   lucEigenvectorsCrossSection*  self           = (lucEigenvectorsCrossSection*)drawingObject;
   FieldVariable*    tensorField    = self->fieldVariable;
   SymmetricTensor   tensor;
   Eigenvector       eigenvectorList[3];
   Dimension_Index   dim_I;
   Index          aIndex, bIndex;

   /* Sample the 2d cross-section */
   lucCrossSection_SampleField(self, False);

   for ( aIndex = 0 ; aIndex < self->resolutionA ; aIndex++ )
   {
      for ( bIndex = 0 ; bIndex < self->resolutionB ; bIndex++ )
      {
         if (self->values[aIndex][bIndex][0] != HUGE_VAL)
         {
            /* Get tensor data & position */
            int t;
            for (t=0; t<tensorField->fieldComponentCount; t++)
               tensor[t] = self->values[aIndex][bIndex][t];

            SymmetricTensor_CalcAllEigenvectors( tensor, dim, eigenvectorList );

            float pos[3] = {self->vertices[aIndex][bIndex][0], self->vertices[aIndex][bIndex][1], self->vertices[aIndex][bIndex][2]};

            if (self->plotEigenVector)
            {
               for ( dim_I = 0 ; dim_I < dim ; dim_I++ )
               {
                  float vec[3] = {eigenvectorList[ dim_I ].vector[0], eigenvectorList[ dim_I ].vector[1], eigenvectorList[ dim_I ].vector[2]};

                  lucDatabase_AddVertices(database, 1, lucVectorType, pos);
                  lucDatabase_AddRGBA(database, lucVectorType, self->opacity, &self->colours[ dim_I ]);
                  if (self->useEigenValue)
                  {
                     vec[0] *= eigenvectorList[ dim_I ].eigenvalue;
                     vec[1] *= eigenvectorList[ dim_I ].eigenvalue;
                     if (dim > 2)
                        vec[2] *= eigenvectorList[ dim_I ].eigenvalue;
                     else
                        vec[2] = 0;
                  }
                  lucDatabase_AddVectors(database, 1, lucVectorType, 0, 1, vec);
               }
            }
            if (self->plotEigenValue)
            {
               float pointSize = 0;

               for ( dim_I = 0 ; dim_I < dim ; dim_I++ )
               {
                  lucDatabase_AddVertices(database, 1, lucShapeType, pos);
                  /* The EigenValue can be negative.... Got to attribute a potential */
                  /* colour for negative values, one for each dim as well */
                  if ( eigenvectorList[ dim_I ].eigenvalue >= 0)
                  {
                     pointSize = eigenvectorList[ dim_I ].eigenvalue;
                     lucDatabase_AddRGBA(database, lucShapeType, self->opacity, &self->colours[ dim_I ]);
                  }
                  else
                  {
                     pointSize = -eigenvectorList[ dim_I ].eigenvalue;
                     lucDatabase_AddRGBA(database, lucShapeType, self->opacity, &self->colourForNegative[ dim_I ]);
                  }
                  lucDatabase_AddValues(database, 1, lucShapeType, lucXWidthData, NULL, &pointSize);
               }
            }
         }
      }
   }

   lucCrossSection_FreeSampleData(self);
}