コード例 #1
0
ファイル: rc5.cpp プロジェクト: KayEss/fost-crypto
void RC5::Base::UncheckedSetKey(const byte *k, unsigned int keylen, const NameValuePairs &params)
{
	AssertValidKeyLength(keylen);

	r = GetRoundsAndThrowIfInvalid(params, this);
	sTable.New(2*(r+1));

	static const RC5_WORD MAGIC_P = 0xb7e15163L;    // magic constant P for wordsize
	static const RC5_WORD MAGIC_Q = 0x9e3779b9L;    // magic constant Q for wordsize
	static const int U=sizeof(RC5_WORD);

	const unsigned int c = STDMAX((keylen+U-1)/U, 1U);	// RC6 paper says c=1 if keylen==0
	SecBlock<RC5_WORD> l(c);

	GetUserKey(LITTLE_ENDIAN_ORDER, l.begin(), c, k, keylen);

	sTable[0] = MAGIC_P;
	for (unsigned j=1; j<sTable.size();j++)
		sTable[j] = sTable[j-1] + MAGIC_Q;

	RC5_WORD a=0, b=0;
	const unsigned n = 3*STDMAX((unsigned int)sTable.size(), c);

	for (unsigned h=0; h < n; h++)
	{
		a = sTable[h % sTable.size()] = rotlConstant<3>((sTable[h % sTable.size()] + a + b));
		b = l[h % c] = rotlMod((l[h % c] + a + b), (a+b));
	}
}
コード例 #2
0
ファイル: rc6.cpp プロジェクト: digitalpeer/turnstile
void RC6::Base::UncheckedSetKey(CipherDir direction, const byte *k, unsigned int keylen, unsigned int rounds)
{
	AssertValidKeyLength(keylen);
	AssertValidRounds(rounds);

	r = rounds;
	sTable.New(2*(r+2));

	static const RC6_WORD MAGIC_P = 0xb7e15163L;    // magic constant P for wordsize
	static const RC6_WORD MAGIC_Q = 0x9e3779b9L;    // magic constant Q for wordsize
	static const int U=sizeof(RC6_WORD);

	const unsigned int c = STDMAX((keylen+U-1)/U, 1U);	// RC6 paper says c=1 if keylen==0
	SecBlock<RC6_WORD> l(c);

	GetUserKey(LITTLE_ENDIAN_ORDER, l.begin(), c, k, keylen);

	sTable[0] = MAGIC_P;
	for (unsigned j=1; j<sTable.size();j++)
		sTable[j] = sTable[j-1] + MAGIC_Q;

	RC6_WORD a=0, b=0;
	const unsigned n = 3*STDMAX((unsigned int)sTable.size(), c);

	for (unsigned h=0; h < n; h++)
	{
		a = sTable[h % sTable.size()] = rotlFixed((sTable[h % sTable.size()] + a + b), 3);
		b = l[h % c] = rotlMod((l[h % c] + a + b), (a+b));
	}
}
コード例 #3
0
ファイル: rc6.cpp プロジェクト: killbug2004/WSProf
RC6Base::RC6Base(const byte *k, unsigned int keylen, unsigned int rounds)
	: r(rounds), sTable((2*r)+4)
{
	assert(keylen == KeyLength(keylen));

	static const RC6_WORD MAGIC_P = 0xb7e15163L;    // magic constant P for wordsize
	static const RC6_WORD MAGIC_Q = 0x9e3779b9L;    // magic constant Q for wordsize
	static const int U=sizeof(RC6_WORD);

	const unsigned int c=(keylen-1)/U + 1;
	SecBlock<RC6_WORD> l(c);

	GetUserKeyLittleEndian(l.ptr, c, k, keylen);

	sTable[0] = MAGIC_P;
	for (unsigned j=1; j<sTable.size;j++)
		sTable[j] = sTable[j-1] + MAGIC_Q;

	RC6_WORD a=0, b=0;
	const unsigned n = 3*STDMAX(sTable.size,c);

	for (unsigned h=0; h < n; h++)
	{
		a = sTable[h % sTable.size] = rotlFixed((sTable[h % sTable.size] + a + b), 3);
		b = l[h % c] = rotlMod((l[h % c] + a + b), (a+b));
	}
}
コード例 #4
0
ファイル: validat1.cpp プロジェクト: 0xmono/miranda-ng
bool TestModeIV(SymmetricCipher &e, SymmetricCipher &d)
{
	SecByteBlock lastIV, iv(e.IVSize());
	StreamTransformationFilter filter(e, new StreamTransformationFilter(d));
	byte plaintext[20480];

	for (unsigned int i=1; i<sizeof(plaintext); i*=2)
	{
		e.GetNextIV(GlobalRNG(), iv);
		if (iv == lastIV)
			return false;
		else
			lastIV = iv;

		e.Resynchronize(iv);
		d.Resynchronize(iv);

		unsigned int length = STDMAX(GlobalRNG().GenerateWord32(0, i), (word32)e.MinLastBlockSize());
		GlobalRNG().GenerateBlock(plaintext, length);

		if (!TestFilter(filter, plaintext, length, plaintext, length))
			return false;
	}

	return true;
}
コード例 #5
0
ファイル: rc6.cpp プロジェクト: AmesianX/encryption-library
RC6Base::RC6Base(const byte *k, unsigned int keylen, unsigned int rounds)
	: r(rounds), sTable((2*r)+4)
{
	static const RC6_WORD MAGIC_P = 0xb7e15163L;    // magic constant P for wordsize
	static const RC6_WORD MAGIC_Q = 0x9e3779b9L;    // magic constant Q for wordsize
	static const int U=sizeof(RC6_WORD);

	const unsigned int c=(keylen-1)/U + 1;
	SecBlock<RC6_WORD> l(c);

	l[(keylen-1)/U] = 0;	// clear top word for when keylen%U != 0
	for (int i = (keylen-1) ; i >= 0; i--)
		l[i/U] = (l[i/U] << 8) + k[i];

	sTable[0] = MAGIC_P;
	for (unsigned j=1; j<sTable.size;j++)
		sTable[j] = sTable[j-1] + MAGIC_Q;

	RC6_WORD a=0, b=0;
	const unsigned n = 3*STDMAX(sTable.size,c);

	for (unsigned h=0; h < n; h++)
	{
		a = sTable[h % sTable.size] = ROTL((sTable[h % sTable.size] + a + b), 3);
		b = l[h % c] = ROTL((l[h % c] + a + b), (a+b));
	}
}
コード例 #6
0
ファイル: stderror.c プロジェクト: jerluc/py-spread
STDINLINE int stderr_output(stderr_action act, int errno_copy, const char *fmt, ...) 
{
  char    buf[STDERR_MAX_ERR_MSG_LEN + 1];
  int     ret1 = 0;
  int     ret2 = 0;
  va_list ap;

  if (stdutil_output != NULL) {
    va_start(ap, fmt);

    ret1      = vsprintf(buf, fmt, ap);  /* write the msg */
    ret1      = STDMAX(ret1, 0);         /* zero out any error */
    buf[ret1] = 0;                       /* ensure termination */

    if (errno_copy != 0) {
      ret2             = sprintf(buf + ret1, ": %s", strerror(errno_copy));   /* errno msg */
      ret2             = STDMAX(ret2, 0);                                     /* zero out */
      buf[ret1 + ret2] = 0;                                                   /* termination */
    }

    if (stdutil_output == (FILE*) 0x1) {
      stdutil_output = stderr;
    }

    fprintf(stdutil_output, "%s\r\n", buf);
    fflush(stdutil_output);

    ret1 += 2;                                                                /* +2 for \r\n */
    va_end(ap);
  }

  if (act == STDERR_EXIT) {
    exit(-1);
  }

  if (act == STDERR_ABORT) {
    abort();
  }

  return ret1 + ret2;
}
コード例 #7
0
ファイル: network.cpp プロジェクト: Jactry/cryptopp
float NetworkSink::ComputeCurrentSpeed()
{
	if (m_speedTimer.ElapsedTime() > 1000)
	{
		m_currentSpeed = m_byteCountSinceLastTimerReset * 1000 / m_speedTimer.ElapsedTime();
		m_maxObservedSpeed = STDMAX(m_currentSpeed, m_maxObservedSpeed * 0.98f);
		m_byteCountSinceLastTimerReset = 0;
		m_speedTimer.StartTimer();
//		OutputDebugString(("max speed: " + IntToString((int)m_maxObservedSpeed) + " current speed: " + IntToString((int)m_currentSpeed) + "\n").c_str());
	}
	return m_currentSpeed;
}
コード例 #8
0
ファイル: stdarr.c プロジェクト: Fran89/seiscomp3
STDINLINE static void stdarr_low_remove_space(stdarr *arr, stdarr_it *it, stdsize num_remove) 
{
  stdsize nsize  = arr->size - num_remove;
  stdsize delta  = num_remove * arr->vsize;
  char *  it_end = it->val + delta;
  stdsize after  = (stdsize) (arr->end - it_end);

  /* shift memory to remove space */

  memmove(it->val, it_end, after);

  arr->end  -= delta;
  arr->size  = nsize;

  /* reallocate if current capacity is too big */

  if ((arr->opts & STDARR_OPTS_NO_AUTO_SHRINK) == 0 &&       /* shrinking allowed */
      nsize <= stdarr_low_capacity(arr) &&                   /* shrinking needed */
      arr->cap != STDARR_MIN_AUTO_ALLOC) {                   /* cap not at min alloc */

    stdsize ncap  = (nsize << 1);
    stdsize prior = (stdsize) (it->val - arr->begin);

    ncap = STDMAX(ncap, STDARR_MIN_AUTO_ALLOC);              /* ensure minimum allocation */

    if (ncap != 0) {                                         /* nsize/ncap can be zero here */
      stdsize asize = ncap * arr->vsize;
      char *  mem;

      if ((mem = (char*) realloc(arr->begin, asize)) == NULL) {
	return;                                              /* couldn't realloc -> that's OK */
      }

      arr->begin = mem;                                      /* fill in new values for 'arr' */
      arr->end   = mem + prior + after;
      arr->cap   = ncap;

    } else {                                                 /* ncap == 0 -> go to zero */
      
      if (arr->begin != NULL) {
	free(arr->begin);
      }

      arr->begin = NULL;
      arr->end   = NULL;
      arr->cap   = 0;
    }

    it->val = arr->begin + prior;                            /* relocate 'it' to erasure point */
  }
}
コード例 #9
0
ファイル: queue.cpp プロジェクト: brolee/EMule-GIFC
byte * ByteQueue::CreatePutSpace(size_t &size)
{
	if (m_lazyLength > 0)
		FinalizeLazyPut();

	if (m_tail->m_tail == m_tail->MaxSize())
	{
		m_tail->next = new ByteQueueNode(STDMAX(m_nodeSize, size));
		m_tail = m_tail->next;
	}

	size = m_tail->MaxSize() - m_tail->m_tail;
	return m_tail->buf + m_tail->m_tail;
}
コード例 #10
0
ファイル: stdarr.c プロジェクト: Fran89/seiscomp3
STDINLINE static stdcode stdarr_low_insert_space(stdarr *arr, stdarr_it *it, stdsize num_insert) 
{
  stdcode ret   = STDESUCCESS;
  stdsize nsize = arr->size + num_insert;
  stdsize delta = num_insert * arr->vsize;
  stdsize after = (stdsize) (arr->end - it->val);

  /* reallocate if current capacity is not big enough */

  if (nsize > stdarr_high_capacity(arr)) {                   /* nsize > X -> nsize > 0 */
    stdsize ncap;
    stdsize prior;
    stdsize asize;
    char *  mem;    

    if ((arr->opts & STDARR_OPTS_NO_AUTO_GROW) != 0) {       /* growth explicitly disallowed */
      ret = STDEACCES;
      goto stdarr_low_insert_space_end;
    }

    ncap  = (nsize << 1);                                    /* nsize > 0 -> ncap > 0 */
    ncap  = STDMAX(ncap, STDARR_MIN_AUTO_ALLOC);             /* ensure minimum allocation */
    asize = ncap * arr->vsize;                               /* calc. alloc size in bytes */
    prior = (stdsize) (it->val - arr->begin);

    if ((mem = (char*) realloc(arr->begin, asize)) == NULL) {
      ret = STDENOMEM;
      goto stdarr_low_insert_space_end;
    }

    arr->begin = mem;                                        /* fill in new values for 'arr' */
    arr->end   = mem + prior + after;
    arr->cap   = ncap;    

    it->val    = mem + prior;                                /* relocate 'it' to insertion point */
  }

  /* shift memory to create space */

  memmove(it->val + delta, it->val, after);

  arr->end  += delta;
  arr->size  = nsize;

 stdarr_low_insert_space_end:
  return ret;
}
コード例 #11
0
std::list<WCIntersectionResult> __WILDCAT_NAMESPACE__::GeometricIntersection(WCGeometricLine *left,
	WCGeometricPoint *right, const WPFloat &tol, const unsigned int &flags) {
	std::list<WCIntersectionResult> results;
	//Get line direction vector
	WCVector4 direction = left->End() - left->Begin();
	//Get the length of the direction vector
	WPFloat dirLen = direction.Magnitude();
	//Get line-being to point vector
	WCVector4 pointVector = right->Data() - left->Begin();
	WPFloat u = direction.DotProduct(pointVector) / (dirLen * dirLen);

	//Bounds check u for [0, 1] - return empty if out of bounds
	if ((u < -tol) || (u > (1.0 + tol))) return results;

	//Do cull boundary check - return if true
	if ((flags & INTERSECT_CULL_BOUNDARY) && ((u < tol) || (1.0 - u < tol) )) return results;

	//Bound u [0.0 to 1.0]
	u = STDMAX( STDMIN(u, 1.0), 0.0);
	//Get the point on the line
	WCVector4 pt = left->Begin() + direction * u;
	//Get distance from point on line to point
	if (pt.Distance(right->Data()) <= tol) {
		//Create intesect result
		WCIntersectionResult hit;
		hit.type = IntersectPoint;
		hit.leftBoundary = (u < tol) || (1.0 - u < tol);
		hit.rightBoundary = true;
		hit.leftParam.I(u);
		hit.rightParam = right->Data();
		//See if genObj
		if (flags & INTERSECT_GEN_POINTS) {
			//Create new point
			WCGeometricPoint *newPoint = new WCGeometricPoint(pt);
			//Add to result struct
			hit.object = newPoint;
		}
		else hit.object = NULL;
		//Add result to the output list
		std::cout << hit << right->Data() << std::endl;
		results.push_back(hit);
	}
	//Return the results
	return results;
}
コード例 #12
0
ファイル: queue.cpp プロジェクト: brolee/EMule-GIFC
size_t ByteQueue::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
{
	if (m_lazyLength > 0)
		FinalizeLazyPut();

	size_t len;
	while ((len=m_tail->Put(inString, length)) < length)
	{
		inString += len;
		length -= len;
		if (m_autoNodeSize && m_nodeSize < s_maxAutoNodeSize)
			do
			{
				m_nodeSize *= 2;
			}
			while (m_nodeSize < length && m_nodeSize < s_maxAutoNodeSize);
		m_tail->next = new ByteQueueNode(STDMAX(m_nodeSize, length));
		m_tail = m_tail->next;
	}

	return 0;
}
コード例 #13
0
ファイル: wait.cpp プロジェクト: axxapp/winxgui
void WaitObjectContainer::AddWriteFd(int fd)
{
	FD_SET(fd, &m_writefds);
	m_maxFd = STDMAX(m_maxFd, fd);
}
コード例 #14
0
ファイル: wait.cpp プロジェクト: axxapp/winxgui
void WaitObjectContainer::AddReadFd(int fd)
{
	FD_SET(fd, &m_readfds);
	m_maxFd = STDMAX(m_maxFd, fd);
}
コード例 #15
0
ファイル: algebra.cpp プロジェクト: 13971643458/qtum
template <class T> T AbstractGroup<T>::CascadeScalarMultiply(const Element &x, const Integer &e1, const Element &y, const Integer &e2) const
{
	const unsigned expLen = STDMAX(e1.BitCount(), e2.BitCount());
	if (expLen==0)
		return this->Identity();

	const unsigned w = (expLen <= 46 ? 1 : (expLen <= 260 ? 2 : 3));
	const unsigned tableSize = 1<<w;
	std::vector<Element> powerTable(tableSize << w);

	powerTable[1] = x;
	powerTable[tableSize] = y;
	if (w==1)
		powerTable[3] = this->Add(x,y);
	else
	{
		powerTable[2] = this->Double(x);
		powerTable[2*tableSize] = this->Double(y);

		unsigned i, j;

		for (i=3; i<tableSize; i+=2)
			powerTable[i] = Add(powerTable[i-2], powerTable[2]);
		for (i=1; i<tableSize; i+=2)
			for (j=i+tableSize; j<(tableSize<<w); j+=tableSize)
				powerTable[j] = Add(powerTable[j-tableSize], y);

		for (i=3*tableSize; i<(tableSize<<w); i+=2*tableSize)
			powerTable[i] = Add(powerTable[i-2*tableSize], powerTable[2*tableSize]);
		for (i=tableSize; i<(tableSize<<w); i+=2*tableSize)
			for (j=i+2; j<i+tableSize; j+=2)
				powerTable[j] = Add(powerTable[j-1], x);
	}

	Element result;
	unsigned power1 = 0, power2 = 0, prevPosition = expLen-1;
	bool firstTime = true;

	for (int i = expLen-1; i>=0; i--)
	{
		power1 = 2*power1 + e1.GetBit(i);
		power2 = 2*power2 + e2.GetBit(i);

		if (i==0 || 2*power1 >= tableSize || 2*power2 >= tableSize)
		{
			unsigned squaresBefore = prevPosition-i;
			unsigned squaresAfter = 0;
			prevPosition = i;
			while ((power1 || power2) && power1%2 == 0 && power2%2==0)
			{
				power1 /= 2;
				power2 /= 2;
				squaresBefore--;
				squaresAfter++;
			}
			if (firstTime)
			{
				result = powerTable[(power2<<w) + power1];
				firstTime = false;
			}
			else
			{
				while (squaresBefore--)
					result = this->Double(result);
				if (power1 || power2)
					Accumulate(result, powerTable[(power2<<w) + power1]);
			}
			while (squaresAfter--)
				result = this->Double(result);
			power1 = power2 = 0;
		}
	}
	return result;
}
コード例 #16
0
ファイル: filters.cpp プロジェクト: prakhs123/cryptopp
void StreamTransformationFilter::FirstPut(const byte* inString)
{
	CRYPTOPP_UNUSED(inString);
	m_optimalBufferSize = m_cipher.OptimalBlockSize();
	m_optimalBufferSize = (unsigned int)STDMAX(m_optimalBufferSize, RoundDownToMultipleOf(4096U, m_optimalBufferSize));
}
コード例 #17
0
ファイル: wait.cpp プロジェクト: ChunHungLiu/Qt-SESAM
void WaitObjectContainer::AddReadFd(int fd, CallStack const& callStack)	// TODO: do something with callStack
{
	CRYPTOPP_UNUSED(callStack);
	FD_SET(fd, &m_readfds);
	m_maxFd = STDMAX(m_maxFd, fd);
}
コード例 #18
0
void StreamTransformationFilter::FirstPut(const byte *inString)
{
	m_optimalBufferSize = m_cipher.OptimalBlockSize();
	m_optimalBufferSize = STDMAX(m_optimalBufferSize, RoundDownToMultipleOf(4096U, m_optimalBufferSize));
}
コード例 #19
0
ファイル: filters.cpp プロジェクト: prakhs123/cryptopp
void StreamTransformationFilter::LastPut(const byte *inString, size_t length)
{
	byte *space = NULL;

	switch (m_padding)
	{
	case NO_PADDING:
	case ZEROS_PADDING:
		if (length > 0)
		{
			size_t minLastBlockSize = m_cipher.MinLastBlockSize();
			bool isForwardTransformation = m_cipher.IsForwardTransformation();

			if (isForwardTransformation && m_padding == ZEROS_PADDING && (minLastBlockSize == 0 || length < minLastBlockSize))
			{
				// do padding
				size_t blockSize = STDMAX(minLastBlockSize, (size_t)m_cipher.MandatoryBlockSize());
				space = HelpCreatePutSpace(*AttachedTransformation(), DEFAULT_CHANNEL, blockSize);
				if (inString) {memcpy(space, inString, length);}
				memset(space + length, 0, blockSize - length);
				m_cipher.ProcessLastBlock(space, space, blockSize);
				AttachedTransformation()->Put(space, blockSize);
			}
			else
			{
				if (minLastBlockSize == 0)
				{
					if (isForwardTransformation)
						throw InvalidDataFormat("StreamTransformationFilter: plaintext length is not a multiple of block size and NO_PADDING is specified");
					else
						throw InvalidCiphertext("StreamTransformationFilter: ciphertext length is not a multiple of block size");
				}

				space = HelpCreatePutSpace(*AttachedTransformation(), DEFAULT_CHANNEL, length, m_optimalBufferSize);
				m_cipher.ProcessLastBlock(space, inString, length);
				AttachedTransformation()->Put(space, length);
			}
		}
		break;

	case PKCS_PADDING:
	case ONE_AND_ZEROS_PADDING:
		unsigned int s;
		s = m_cipher.MandatoryBlockSize();
		assert(s > 1);
		space = HelpCreatePutSpace(*AttachedTransformation(), DEFAULT_CHANNEL, s, m_optimalBufferSize);
		if (m_cipher.IsForwardTransformation())
		{
			assert(length < s);
			if (inString) {memcpy(space, inString, length);}
			if (m_padding == PKCS_PADDING)
			{
				assert(s < 256);
				byte pad = byte(s-length);
				memset(space+length, pad, s-length);
			}
			else
			{
				space[length] = 0x80;
				memset(space+length+1, 0, s-length-1);
			}
			m_cipher.ProcessData(space, space, s);
			AttachedTransformation()->Put(space, s);
		}
		else
		{
			if (length != s)
				throw InvalidCiphertext("StreamTransformationFilter: ciphertext length is not a multiple of block size");
			m_cipher.ProcessData(space, inString, s);
			if (m_padding == PKCS_PADDING)
			{
				byte pad = space[s-1];
				if (pad < 1 || pad > s || std::find_if(space+s-pad, space+s, std::bind2nd(std::not_equal_to<byte>(), pad)) != space+s)
					throw InvalidCiphertext("StreamTransformationFilter: invalid PKCS #7 block padding found");
				length = s-pad;
			}
			else
			{
				while (length > 1 && space[length-1] == 0)
					--length;
				if (space[--length] != 0x80)
					throw InvalidCiphertext("StreamTransformationFilter: invalid ones-and-zeros padding found");
			}
			AttachedTransformation()->Put(space, length);
		}
		break;

	default:
		assert(false);
	}
}
コード例 #20
0
std::list<WCIntersectionResult> __WILDCAT_NAMESPACE__::GeometricIntersection(WCGeometricLine *left,
	WCGeometricLine *right, const WPFloat &tol, unsigned const int &flags) {
	std::list<WCIntersectionResult> results;
	//Check if self intersecting
	if (left == right) return results;

	//Evaluate both lines at both ends
	WCVector4 p1 = left->Begin();
	WCVector4 p2 = left->End();
	WCVector4 p3 = right->Begin();
	WCVector4 p4 = right->End();
	
	//Get length vectors
	WCVector4 p21(p2 - p1);
	WCVector4 p43(p4 - p3);
	WPFloat dist;
	
	//Check for parallel
	WCVector4 cross = p43.CrossProduct(p21);
	WPFloat denom = cross.Magnitude();
	WPFloat onePlusTol = 1.0 + tol;
	if (denom < 0.001) {
		//Determine perpendicular distance
		WPFloat p21mag = p21.Magnitude();
		WCVector4 p13(p1 - p3);
		WCVector4 num = p21.CrossProduct(p13);
		dist = num.Magnitude() / p21mag;
		//If outside of tolerance, no intersection
		if (dist <= tol) {
			//Project all four points onto opposite lines
			p21 /= p21mag;
			WPFloat p43mag = p43.Magnitude();
			p43 /= p43mag;
			WCVector4 p31(p3 - p1);
			WCVector4 p41(p4 - p1);
			WCVector4 p23(p2 - p3);
			WPFloat p1Proj = p43.DotProduct(p13) / p43mag;
			WPFloat p2Proj = p43.DotProduct(p23) / p43mag;
			WPFloat p3Proj = p21.DotProduct(p31) / p21mag;
			WPFloat p4Proj = p21.DotProduct(p41) / p21mag;

			//See if each point is in 0.0 to 1.0 +- tolerance
			bool p1In = (p1Proj > -tol) && (p1Proj < onePlusTol);
			bool p2In = (p2Proj > -tol) && (p2Proj < onePlusTol);
			bool p3In = (p3Proj > -tol) && (p3Proj < onePlusTol);
			bool p4In = (p4Proj > -tol) && (p4Proj < onePlusTol);
			//If none are in then no overlap, return
			if ( p1In || p2In || p3In || p4In) {
				//Bounds check all of the projections
				p1Proj = STDMAX(0.0, STDMIN(1.0, p1Proj));
				p2Proj = STDMAX(0.0, STDMIN(1.0, p2Proj));
				p3Proj = STDMAX(0.0, STDMIN(1.0, p3Proj));
				p4Proj = STDMAX(0.0, STDMIN(1.0, p4Proj));
				//Create hit result
				WCIntersectionResult hit;
				hit.type = IntersectLine;
				WCVector4 pStart, pEnd;
				//Case 1 - Left is completely within Right
				if (p1In && p2In) {
					//Set the start and end points
					pStart = p1;
					pEnd = p2;
					//Set the param values
					hit.leftParam.I( 0.0 );
					hit.leftParam.J( 1.0 );
					hit.rightParam.I( STDMIN(p1Proj,p2Proj) );
					hit.rightParam.J( STDMAX(p1Proj,p2Proj) );
					//Set boundary values
					hit.leftBoundary = true;
					hit.rightBoundary = false;
				}
				//Case 2 - Right is completely within Left
				else if (p3In && p4In) {
					//Set the start and end points
					pStart = p3;
					pEnd = p4;
					//Set the param values
					hit.leftParam.I( STDMIN(p3Proj,p4Proj) );
					hit.leftParam.J( STDMAX(p3Proj,p4Proj) );
					hit.rightParam.I( 0.0 );
					hit.rightParam.J( 1.0 );
					//Set boundary values
					hit.leftBoundary = false;
					hit.rightBoundary = true;
				}
				//Remaining Cases
				else {
					//Simple sets
					if (p1In) { hit.leftParam.I(0.0); pStart = p1; }
					if (p2In) { hit.leftParam.J(1.0); pStart = p2; }
					if (p3In) { hit.rightParam.I(0.0); pEnd = p3; }
					if (p4In) { hit.rightParam.J(1.0); pEnd = p4; }
					//Double sets
					if (p1In && p3In) { hit.leftParam.J(p3Proj); hit.rightParam.J(p1Proj); }
					if (p1In && p4In) { hit.leftParam.J(p4Proj); hit.rightParam.I(p1Proj); }
					if (p2In && p3In) { hit.leftParam.I(p3Proj); hit.rightParam.J(p2Proj); }
					if (p2In && p4In) { hit.leftParam.I(p4Proj); hit.rightParam.I(p2Proj); }
					
					//Check to see if intersection is a point (think of two lines end to end)
					if (pStart.Distance(pEnd) < tol) hit.type = IntersectPoint;

					//This is not the best way to check for CULL BOUNDARY, but if pStart and pEnd distance < tol, must be end-to-end
					if ((flags & INTERSECT_CULL_BOUNDARY) && (hit.type == IntersectPoint))
						return results;

					//Set boundary values
					hit.leftBoundary = true;
					hit.rightBoundary = true;
				}
				//Overlap forces boundarys to false

				//See if genObj
				if ((hit.type == IntersectPoint) && (flags & INTERSECT_GEN_POINTS)) {
					//Create new point
					WCGeometricPoint *newPoint = new WCGeometricPoint(pStart);
					//Add to result struct
					hit.object = newPoint;					
				}
				else if ((hit.type == IntersectLine) && (flags & INTERSECT_GEN_LINES)) {
					//Create new line
					WCGeometricLine *newLine = new WCGeometricLine(pStart, pEnd);
					//Add to result struct
					hit.object = newLine;
				}
				else hit.object = NULL;
				//Add the hit to the list
				results.push_back(hit);
			}
		}
	}
	//Non-Parallel Case
	else {
		//Many dot products
		WPFloat d121 = p1.DotProduct(p21);			// L
		WPFloat d2121 = p21.DotProduct(p21);		// M
		WPFloat d321 = p3.DotProduct(p21);			// N
		WPFloat d4321 = p43.DotProduct(p21);		// O
		WPFloat d143 = p1.DotProduct(p43);			// Q
		WPFloat d343 = p3.DotProduct(p43);			// R
		WPFloat d4343 = p43.DotProduct(p43);		// S
		denom = (d4343 * d2121) - (d4321 * d4321);
		//What does this correspond to?
		if (denom == 0.0) {
			CLOGGER_WARN(WCLogManager::RootLogger(), "GeometricIntersection::LineLine -  Denominator == 0.0.");
		}
		//Otherwise
		else {
			//Calculate parametric intersection values
			WPFloat numer = d4343 * (d321 - d121) + d4321 * (d143 - d343);
			WPFloat mua = numer / denom;
			WPFloat mub = (d143 + d4321 * mua - d343) / d4343;
			//Make sure mua and mub are (0.0 +- tol, 1.0 +- tol)
			if ( (mua < onePlusTol) && (mua > -tol) && (mub < onePlusTol) && (mub > -tol) ) {
				//Bound mua and mub [0, 1]
				mua = STDMAX(0.0, STDMIN(1.0, mua));
				mub = STDMAX(0.0, STDMIN(1.0, mub));
				//Calculate points and distance between them
				WCVector4 pointOnFirst = p1 + p21 * mua;
				WCVector4 pointOnSecond = p3 + p43 * mub;
				//Make sure lines are not > tol apart
				dist = pointOnFirst.Distance(pointOnSecond);
				if (dist < tol) {
					//Create intersection result
					WCIntersectionResult hit;
					hit.type = IntersectPoint;
					hit.leftParam.I(mua);
					hit.rightParam.I(mub);
					hit.leftBoundary = (fabs(mua) < tol) || (fabs(mua-1.0) < tol);
					hit.rightBoundary = (fabs(mub) < tol) || (fabs(mub-1.0) < tol);

					//Check for culling end-point intersections
					if (!(flags & INTERSECT_CULL_BOUNDARY) || (!hit.leftBoundary && !hit.rightBoundary)) {
						//See if genObj
						if (flags & INTERSECT_GEN_POINTS) {
							//Create new point
							WCGeometricPoint *newPoint = new WCGeometricPoint(pointOnFirst);
							//Add to result struct
							hit.object = newPoint;
						}
						else hit.object = NULL;
						//Add the intersection to the list
//						std::cout << hit << pointOnFirst << std::endl;
						results.push_back(hit);
					}
				}
			}
		}
	}
	//Return result
	return results;
}
コード例 #21
0
ファイル: wait.cpp プロジェクト: tweimer/miranda-ng
void WaitObjectContainer::AddWriteFd(int fd, CallStack const& callStack) // TODO: do something with callStack
{
	FD_SET(fd, &m_writefds);
	m_maxFd = STDMAX(m_maxFd, fd);
}