//! Elimina i segmenti che non sono contenuti all'interno dello stroke!!!
  void checkSegments(std::vector<TAutocloser::Segment> &segments,
                     TStroke *stroke, const TRasterCM32P &ras,
                     const TPoint &delta) {
    TVectorImage vi;
    TStroke *app = new TStroke();
    *app         = *stroke;
    app->transform(TTranslation(convert(ras->getCenter())));
    vi.addStroke(app);
    vi.findRegions();
    std::vector<TAutocloser::Segment>::iterator it = segments.begin();
    for (; it < segments.end(); it++) {
      if (it == segments.end()) break;

      int i;
      bool isContained = false;
      for (i = 0; i < (int)vi.getRegionCount(); i++) {
        TRegion *reg = vi.getRegion(i);
        if (reg->contains(convert(it->first + delta)) &&
            reg->contains(convert(it->second + delta))) {
          isContained = true;
          break;
        }
      }
      if (!isContained) {
        it = segments.erase(it);
        if (it != segments.end() && it != segments.begin())
          it--;
        else if (it == segments.end())
          break;
      }
    }
  }
void DragSelectionTool::RasterDeformTool::applyTransform(FourPoints bbox)
{
	RasterSelectionTool *tool = (RasterSelectionTool *)getTool();
	tool->setNewFreeDeformer();
	if (!m_deformUndo)
		m_deformUndo = new UndoRasterDeform(tool);
	RasterSelection *selection = dynamic_cast<RasterSelection *>(tool->getSelection());
	assert(selection);
	FourPoints realBbox = bbox * selection->getTransformation().inv();
	RasterFreeDeformer *freeDeformer = (RasterFreeDeformer *)tool->getFreeDeformer();
	freeDeformer->setNoAntialiasing(tool->getNoAntialiasingValue());
	freeDeformer->setPoints(realBbox.getP00(), realBbox.getP10(), realBbox.getP11(), realBbox.getP01());
	freeDeformer->deformImage();
	selection->setFloatingSeletion(freeDeformer->getImage());
	VectorFreeDeformer *vectorFreeDeformer = tool->getSelectionFreeDeformer();
	if (vectorFreeDeformer) {
		vectorFreeDeformer->setPoints(realBbox.getP00(), realBbox.getP10(), realBbox.getP11(), realBbox.getP01());
		vectorFreeDeformer->deformImage();
		TVectorImage *vi = vectorFreeDeformer->getDeformedImage();
		std::vector<TStroke> newStrokes;
		int i;
		for (i = 0; i < (int)vi->getStrokeCount(); i++)
			newStrokes.push_back(*(vi->getStroke(i)));
		selection->setStrokes(newStrokes);
	}
	tool->m_deformValues.m_isSelectionModified = true;
	if (!m_isDragging)
		tool->notifyImageChanged();
}
void RasterSelectionTool::setNewFreeDeformer()
{
	if (!m_freeDeformers.empty() || isSelectionEmpty())
		return;

	TImageP image = (TImageP)getImage(true);

	TToonzImageP ti = (TToonzImageP)image;
	TRasterImageP ri = (TRasterImageP)image;
	if (!ti && !ri)
		return;

	if (!isFloating())
		m_rasterSelection.makeFloating();
	m_freeDeformers.push_back(new RasterFreeDeformer(m_rasterSelection.getFloatingSelection()));
	std::vector<TStroke> strokes = m_rasterSelection.getOriginalStrokes();
	if (!strokes.empty()) {
		TVectorImage *vi = new TVectorImage();
		std::set<int> indices;
		//Devo deformare anche gli strokes della selezione!!!
		int i;
		for (i = 0; i < (int)strokes.size(); i++) {
			vi->addStroke(new TStroke(strokes[i]));
			indices.insert(i);
		}
		m_selectionFreeDeformer = new VectorFreeDeformer(vi, indices);
		m_selectionFreeDeformer->setPreserveThickness(true);
	}
}
		static void selectStyles(const TVectorImage &vi, const std::set<int> &styles,
								 std::vector<int> &strokes)
		{
			UINT s, sCount = vi.getStrokeCount();
			for (s = 0; s != sCount; ++s) {
				if (styles.count(vi.getStroke(s)->getStyle()))
					strokes.push_back(s);
			}
		}
std::vector<int> getSelectedStrokes(TVectorImage &vi, const LevelSelection &levelSelection)
{
	struct locals {

		static void selectStyles(const TVectorImage &vi, const std::set<int> &styles,
								 std::vector<int> &strokes)
		{
			UINT s, sCount = vi.getStrokeCount();
			for (s = 0; s != sCount; ++s) {
				if (styles.count(vi.getStroke(s)->getStyle()))
					strokes.push_back(s);
			}
		}
	}; // locals

	std::vector<int> strokes;

	switch (levelSelection.filter()) {
	case LevelSelection::EMPTY:;

		CASE LevelSelection::WHOLE : strokes.assign(boost::make_counting_iterator(0u),
													boost::make_counting_iterator(vi.getStrokeCount()));

		CASE LevelSelection::SELECTED_STYLES : locals::selectStyles(vi, levelSelection.styles(), strokes);

		CASE LevelSelection::BOUNDARY_STROKES : getBoundaries(vi, strokes);
	}

	return strokes;
}
Beispiel #6
0
void stroke_autofill_learn(const TVectorImageP &imgToLearn, TStroke *stroke) {
  if (!imgToLearn || !stroke || stroke->getControlPointCount() == 0) return;
  TVectorImage appImg;
  TStroke *appStroke = new TStroke(*stroke);
  appImg.addStroke(appStroke);
  appImg.findRegions();

  double pbx, pby;
  double totalArea = 0;
  pbx = pby = 0;

  if (!regionsReference.isEmpty()) regionsReference.clear();

  int i, j, index = 0;

  for (i = 0; i < (int)imgToLearn->getRegionCount(); i++) {
    TRegion *currentRegion = imgToLearn->getRegion(i);
    for (j = 0; j < (int)appImg.getRegionCount(); j++) {
      TRegion *region = appImg.getRegion(j);
      if (contains(region, currentRegion)) {
        scanRegion(currentRegion, index, regionsReference, region->getBBox());
        index++;
        int k, subRegionCount = currentRegion->getSubregionCount();
        for (k = 0; k < subRegionCount; k++) {
          TRegion *subRegion = currentRegion->getSubregion(k);
          if (contains(region, subRegion))
            scanSubRegion(subRegion, index, regionsReference,
                          region->getBBox());
        }
      }
    }
  }

  QMap<int, Region>::Iterator it;
  for (it = regionsReference.begin(); it != regionsReference.end(); it++) {
    pbx += it.value().m_barycentre.x;
    pby += it.value().m_barycentre.y;
    totalArea += it.value().m_area;
  }

  if (totalArea > 0)
    referenceB = TPointD(pbx / totalArea, pby / totalArea);
  else
    referenceB = TPointD(0.0, 0.0);
}
Beispiel #7
0
void AreaFiller::strokeFill(TStroke *stroke, int colorId, bool onlyUnfilled,
                            bool fillPaints, bool fillInks) {
  stroke->transform(TTranslation(convert(m_ras->getCenter())));
  m_ras->lock();

  std::vector<std::pair<TPoint, int>> seeds;
  computeSeeds(m_ras, stroke, seeds);

  TVectorImage app;
  app.addStroke(stroke);
  app.findRegions();
  for (UINT i = 0; i < app.getRegionCount(); i++)
    fillArea(m_ras, app.getRegion(i), colorId, onlyUnfilled, fillPaints,
             fillInks);
  app.removeStroke(0);

  stroke->transform(TTranslation(convert(-m_ras->getCenter())));
  restoreColors(m_ras, seeds);
  m_ras->unlock();
}
Beispiel #8
0
bool stroke_autofill_apply(const TVectorImageP &imgToApply, TStroke *stroke,
                           bool selective) {
  if (!imgToApply || !stroke || stroke->getControlPointCount() == 0)
    return false;
  TVectorImage appImg;
  TStroke *appStroke = new TStroke(*stroke);
  appImg.addStroke(appStroke);
  appImg.findRegions();

  if (regionsReference.size() <= 0) return false;

  double pbx, pby;
  double totalArea = 0;
  pbx = pby = 0.0;

  if (!regionsWork.isEmpty()) regionsWork.clear();

  int i, j, index = 0;

  for (i = 0; i < (int)imgToApply->getRegionCount(); i++) {
    TRegion *currentRegion = imgToApply->getRegion(i);
    for (j = 0; j < (int)appImg.getRegionCount(); j++) {
      TRegion *region = appImg.getRegion(j);
      if (contains(region, currentRegion)) {
        scanRegion(currentRegion, index, regionsWork, region->getBBox());
        index++;
        int k, subRegionCount = currentRegion->getSubregionCount();
        for (k = 0; k < subRegionCount; k++) {
          TRegion *subRegion = currentRegion->getSubregion(k);
          if (contains(region, subRegion))
            scanSubRegion(subRegion, index, regionsWork, region->getBBox());
        }
      }
    }
  }

  if (regionsWork.size() <= 0) return false;

  QMap<int, Region>::Iterator it;
  for (it = regionsWork.begin(); it != regionsWork.end(); it++) {
    pbx += it.value().m_barycentre.x;
    pby += it.value().m_barycentre.y;
    totalArea += it.value().m_area;
  }

  workB = TPointD(pbx / totalArea, pby / totalArea);

  std::vector<MatchingProbs> probVector;

  RegionDataList::Iterator refIt, workIt;
  for (refIt = regionsReference.begin(); refIt != regionsReference.end();
       refIt++)
    for (workIt = regionsWork.begin(); workIt != regionsWork.end(); workIt++)
      assignProbs(probVector, refIt.value(), workIt.value(), refIt.key(),
                  workIt.key());

  bool filledRegions = false;
  for (refIt = regionsReference.begin(); refIt != regionsReference.end();
       refIt++) {
    int to = 0, from = 0;
    int valore = 0;
    do
      valore = match(probVector, from, to);
    while ((regionsWork[to].m_match != -1 ||
            regionsReference[from].m_match != -1) &&
           valore > 0);
    if (valore > AMB_TRESH) {
      regionsWork[to].m_match        = from;
      regionsReference[from].m_match = to;
      regionsWork[to].m_styleId      = regionsReference[from].m_styleId;
      TRegion *reg                   = regionsWork[to].m_region;
      if (reg && (!selective || selective && reg->getStyle() == 0)) {
        reg->setStyle(regionsWork[to].m_styleId);
        filledRegions = true;
      }
    }
  }
  return filledRegions;
}