int ccFastMarchingForNormsDirection::step()
{
	if (!initialized)
	{
		//printf("Not yet initialized !");
		return -1;
	}

	unsigned minTCellIndex = getNearestTrialCell();

	if (minTCellIndex==0)
	{
		//fl_alert("No more trial cells !");
		return 0;
	}

	//printf("minTCellIndex=%i\n",minTCellIndex);

	CCLib::FastMarching::Cell* minTCell =  theGrid[minTCellIndex];
	assert(minTCell != NULL);

	assert(minTCell->state != CCLib::FastMarching::Cell::ACTIVE_CELL);

	if (minTCell->T < FM_INF)
	{
		//on rajoute cette cellule au groupe des cellules "ACTIVE"
		minTCell->state = CCLib::FastMarching::Cell::ACTIVE_CELL;
		activeCells.push_back(minTCellIndex);

		//printf("Cell %i added to ACTIVE\n",minTCellIndex);
		//fprintf(fp,"%f %f %f\n",((DirectionCell*)minTCell)->f,minTCell->T,(minTCell->T-lastT)/cellSize);

		lastT = minTCell->T;

		//on doit rajouter ses voisines au groupe TRIAL
		unsigned nIndex;
		CCLib::FastMarching::Cell* nCell;
		for (int i=0;i<CC_FM_NUMBER_OF_NEIGHBOURS;++i)
		{
			nIndex = minTCellIndex + neighboursIndexShift[i];
			//pointeur vers la cellule voisine
			nCell = theGrid[nIndex];

			//si elle est définie
			if (nCell)
			{
				//et si elle n'est pas encore dans un groupe, on la rajoute
				if (nCell->state==CCLib::FastMarching::Cell::FAR_CELL)
				{
					nCell->state = CCLib::FastMarching::Cell::TRIAL_CELL;
					nCell->T = computeT(nIndex);

					addTrialCell(nIndex,nCell->T);
					//Console::print("Cell %i added to TRIAL\n",nIndex);
				}
				else if (nCell->state == CCLib::FastMarching::Cell::TRIAL_CELL)
				//sinon, il faut recaculer T
				{
					float t_old = nCell->T;
					float t_new = computeT(nIndex);

					if (t_new<t_old)
						nCell->T = t_new;
				}
			}
		}
	}

	return 1;
}
int ccFastMarchingForNormsDirection::step()
{
	if (!m_initialized)
		return -1;

	//get 'earliest' cell
	unsigned minTCellIndex = getNearestTrialCell();
	if (minTCellIndex == 0)
		return 0;

	CCLib::FastMarching::Cell* minTCell = m_theGrid[minTCellIndex];
	assert(minTCell && minTCell->state != DirectionCell::ACTIVE_CELL);

	if (minTCell->T < Cell::T_INF())
	{
#ifdef QT_DEBUG
		if (s_cellIndex == 0)
		{
			//process seed cells first!
			for (size_t i=0; i<m_activeCells.size(); ++i)
				static_cast<DirectionCell*>(m_theGrid[m_activeCells[i]])->scalar = static_cast<float>(0);
			s_cellIndex++;
		}
		static_cast<DirectionCell*>(minTCell)->scalar = static_cast<float>(s_cellIndex++);
#endif

		//resolve the cell orientation
		resolveCellOrientation(minTCellIndex);
		//we add this cell to the "ACTIVE" set
		addActiveCell(minTCellIndex);

		//add its neighbors to the TRIAL set
		for (unsigned i=0;i<m_numberOfNeighbours;++i)
		{
			//get neighbor cell
			unsigned nIndex = minTCellIndex + m_neighboursIndexShift[i];
			CCLib::FastMarching::Cell* nCell = m_theGrid[nIndex];
			if (nCell)
			{
				//if it' not yet a TRIAL cell
				if (nCell->state == DirectionCell::FAR_CELL)
				{
					nCell->T = computeT(nIndex);
					addTrialCell(nIndex);
				}
				//otherwise we must update it's arrival time
				else if (nCell->state == DirectionCell::TRIAL_CELL)
				{
					const float& t_old = nCell->T;
					float t_new = computeT(nIndex);

					if (t_new < t_old)
						nCell->T = t_new;
				}
			}
		}
	}
	else
	{
		addIgnoredCell(minTCellIndex);
	}

	return 1;
}
int FastMarchingForFacetExtraction::step()
{
	if (!m_initialized)
		return -1;

	//get 'earliest' cell
	unsigned minTCellIndex = getNearestTrialCell();
	if (minTCellIndex == 0)
		return 0;

	CCLib::FastMarching::Cell* minTCell =  m_theGrid[minTCellIndex];
	assert(minTCell && minTCell->state != PlanarCell::ACTIVE_CELL);

	if (minTCell->T < Cell::T_INF())
	{
		assert(m_currentFacetPoints);
		unsigned sizeBefore = m_currentFacetPoints->size();

		//check if we can add the cell to the current "ACTIVE" set
		ScalarType error = addCellToCurrentFacet(minTCellIndex);

		if (error >= 0)
		{
			if (error > m_maxError)
			{
				//resulting error would be too high
				m_currentFacetPoints->resize(sizeBefore);
				//we leave the cell as is (in the EMPTY state)
				//so that we won't look at it again!
				addIgnoredCell(minTCellIndex);
			}
			else
			{
				m_currentFacetError = error;

				//add the cell to the "ACTIVE" set
				addActiveCell(minTCellIndex);

				//add its neighbors to the TRIAL set
				for (unsigned i=0; i<m_numberOfNeighbours; ++i)
				{
					//get neighbor cell
					unsigned nIndex = minTCellIndex + m_neighboursIndexShift[i];
					CCLib::FastMarching::Cell* nCell = m_theGrid[nIndex];
					if (nCell)
					{
						//if it' not yet a TRIAL cell
						if (nCell->state == PlanarCell::FAR_CELL)
						{
							nCell->T = computeT(nIndex);
							addTrialCell(nIndex);
						}
						//otherwise we must update it's arrival time
						else if (nCell->state == PlanarCell::TRIAL_CELL)
						{
							const float& t_old = nCell->T;
							float t_new = computeT(nIndex);

							if (t_new < t_old)
								nCell->T = t_new;
						}
					}
				}
			}
		}
		else
		{
			//an error occurred
			return -1;
		}
	}
	else
	{
		addIgnoredCell(minTCellIndex);
	}

	return 1;
}