Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/tpinnedrangeset.h"
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPinnedRangeSet::TPinnedRangeSet()
Toshihiro Shimizu 890ddd
	: m_stageObject()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPinnedRangeSet::getRangeIndex(int frame) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_ranges.empty())
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	if (frame < m_ranges.front().first || m_ranges.back().second < frame)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	int start = 0, end = (int)m_ranges.size() - 1;
Toshihiro Shimizu 890ddd
	while (start < end) {
Toshihiro Shimizu 890ddd
		if (start + 1 == end) {
Toshihiro Shimizu 890ddd
			if (m_ranges[end].first <= frame)
Toshihiro Shimizu 890ddd
				start = end;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				end = start;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		int m = (start + end) / 2;
Toshihiro Shimizu 890ddd
		assert(start < m && m < end);
Toshihiro Shimizu 890ddd
		if (m_ranges[m].first <= frame)
Toshihiro Shimizu 890ddd
			start = m;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			end = m;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int index = start;
Toshihiro Shimizu 890ddd
	if (m_ranges[index].first <= frame && frame <= m_ranges[index].second)
Toshihiro Shimizu 890ddd
		return index;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPinnedRangeSet::setRange(int first, int second)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::vector<range>::iterator it;</range>
Toshihiro Shimizu 890ddd
	// finds the range which could possibly be merged before : range.second+1 >= first
Toshihiro Shimizu 890ddd
	for (it = m_ranges.begin(); it != m_ranges.end(); ++it)
Toshihiro Shimizu 890ddd
		if (it->second + 1 >= first)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (it == m_ranges.end()) {
Toshihiro Shimizu 890ddd
		// not found => for every range : range.second+1 < first. Just add the new one
Toshihiro Shimizu 890ddd
		m_ranges.push_back(Range(first, second));
Toshihiro Shimizu 890ddd
	} else if (it->first > second + 1) {
Toshihiro Shimizu 890ddd
		// it comes after and can not be merged. Just insert the new one
Toshihiro Shimizu 890ddd
		m_ranges.insert(it, Range(first, second));
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		// merge
Toshihiro Shimizu 890ddd
		if (first < it->first)
Toshihiro Shimizu 890ddd
			it->first = first;
Toshihiro Shimizu 890ddd
		// update it->second, and find all the ranges which should be deleted
Shinya Kitaoka 3bfa54
		std::vector<range>::iterator it2 = it;</range>
Toshihiro Shimizu 890ddd
		for (++it2; it2 != m_ranges.end(); ++it2)
Toshihiro Shimizu 890ddd
			if (it2->first > second + 1)
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				it->second = it2->second;
Toshihiro Shimizu 890ddd
		if (second > it->second)
Toshihiro Shimizu 890ddd
			it->second = second;
Toshihiro Shimizu 890ddd
		++it;
Toshihiro Shimizu 890ddd
		if (it != it2)
Toshihiro Shimizu 890ddd
			m_ranges.erase(it, it2);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPinnedRangeSet::removeRange(int first, int second)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::vector<range>::iterator it;</range>
Toshihiro Shimizu 890ddd
	// finds the first range which could possibly overlap [first,second] : range.second>=first
Toshihiro Shimizu 890ddd
	for (it = m_ranges.begin(); it != m_ranges.end(); ++it)
Toshihiro Shimizu 890ddd
		if (it->second >= first)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (it == m_ranges.end() || it->first > second) {
Toshihiro Shimizu 890ddd
		// nothing to do: for each range range.second < first or range.first > second
Toshihiro Shimizu 890ddd
	} else if (it->first < first && it->second > second) {
Toshihiro Shimizu 890ddd
		// [first,second] is a "hole"
Toshihiro Shimizu 890ddd
		Range range(it->first, first - 1);
Toshihiro Shimizu 890ddd
		it->first = second + 1;
Toshihiro Shimizu 890ddd
		m_ranges.insert(it, range);
Toshihiro Shimizu 890ddd
	} else {
Shinya Kitaoka 3bfa54
		std::vector<range>::iterator it2;</range>
Toshihiro Shimizu 890ddd
		if (it->first < first) {
Toshihiro Shimizu 890ddd
			it->second = first - 1;
Toshihiro Shimizu 890ddd
			++it;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		it2 = it;
Toshihiro Shimizu 890ddd
		while (it != m_ranges.end() && it->second <= second)
Toshihiro Shimizu 890ddd
			++it;
Toshihiro Shimizu 890ddd
		if (it != m_ranges.end() && it->first <= second)
Toshihiro Shimizu 890ddd
			it->first = second + 1;
Toshihiro Shimizu 890ddd
		if (it2 != it)
Toshihiro Shimizu 890ddd
			m_ranges.erase(it2, it);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPinnedRangeSet::removeAllRanges()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_ranges.clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPinnedRangeSet::setPlacement(const TAffine &placement)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_placement = placement;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPinnedRangeSet *TPinnedRangeSet::clone()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPinnedRangeSet *clonedPinnedRangeSet = new TPinnedRangeSet();
Toshihiro Shimizu 890ddd
	clonedPinnedRangeSet->m_stageObject = m_stageObject;
Toshihiro Shimizu 890ddd
	clonedPinnedRangeSet->m_placement = m_placement;
Toshihiro Shimizu 890ddd
	clonedPinnedRangeSet->m_ranges = m_ranges;
Toshihiro Shimizu 890ddd
	return clonedPinnedRangeSet;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPinnedRangeSet::loadData(TIStream &is)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_ranges.clear();
Toshihiro Shimizu 890ddd
	int prevFrame = 0;
Toshihiro Shimizu 890ddd
	int i = 0;
Shinya Kitaoka 3bfa54
	std::string tagName;
Toshihiro Shimizu 890ddd
	int count = 0;
Toshihiro Shimizu 890ddd
	while (is.matchTag(tagName) && count < 3) {
Toshihiro Shimizu 890ddd
		if (tagName == "permanent") {
Toshihiro Shimizu 890ddd
			while (!is.matchEndTag()) {
Toshihiro Shimizu 890ddd
				i++;
Toshihiro Shimizu 890ddd
				int frame = 0;
Toshihiro Shimizu 890ddd
				is >> frame;
Toshihiro Shimizu 890ddd
				if (i % 2 == 0)
Toshihiro Shimizu 890ddd
					m_ranges.push_back(Range(prevFrame, frame));
Toshihiro Shimizu 890ddd
				prevFrame = frame;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			count++;
Toshihiro Shimizu 890ddd
		} else if (tagName == "temp") {
Toshihiro Shimizu 890ddd
			assert(0);
Toshihiro Shimizu 890ddd
			// OBSOLETO
Toshihiro Shimizu 890ddd
			while (!is.matchEndTag()) {
Toshihiro Shimizu 890ddd
				int frame = 0;
Toshihiro Shimizu 890ddd
				is >> frame;
Toshihiro Shimizu 890ddd
				//m_tempPinned.push_back(frame);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			count++;
Toshihiro Shimizu 890ddd
		} else if (tagName == "lockedAngle") {
Toshihiro Shimizu 890ddd
			assert(0);
Toshihiro Shimizu 890ddd
			// OBSOLETO
Toshihiro Shimizu 890ddd
			while (!is.matchEndTag()) {
Toshihiro Shimizu 890ddd
				int rangeIndex = -1;
Toshihiro Shimizu 890ddd
				is >> rangeIndex;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			count++;
Toshihiro Shimizu 890ddd
		} else if (tagName == "placement") {
Toshihiro Shimizu 890ddd
			is >> m_placement.a11 >> m_placement.a12 >> m_placement.a13;
Toshihiro Shimizu 890ddd
			is >> m_placement.a21 >> m_placement.a22 >> m_placement.a23;
Toshihiro Shimizu 890ddd
			is.matchEndTag();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPinnedRangeSet::saveData(TOStream &os)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if ((int)m_ranges.size() == 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	os.openChild("pinnedStatus");
Toshihiro Shimizu 890ddd
	if (m_ranges.size() > 0) {
Toshihiro Shimizu 890ddd
		os.openChild("permanent");
Toshihiro Shimizu 890ddd
		for (int p = 0; p < (int)m_ranges.size(); p++) {
Toshihiro Shimizu 890ddd
			os << m_ranges[p].first << m_ranges[p].second;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		os.closeChild();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (m_placement != TAffine()) {
Toshihiro Shimizu 890ddd
		os.openChild("placement");
Toshihiro Shimizu 890ddd
		os << m_placement.a11 << m_placement.a12 << m_placement.a13;
Toshihiro Shimizu 890ddd
		os << m_placement.a21 << m_placement.a22 << m_placement.a23;
Toshihiro Shimizu 890ddd
		os.closeChild();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	os.closeChild();
Toshihiro Shimizu 890ddd
}